压缩代码的最佳实践? [英] Best practice for condensing code?

查看:68
本文介绍了压缩代码的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您希望将一个方法压缩为一个简单的调用(出于简洁或易读性),则该方法中的代码(目标-c)是首选的做法。

If you have code (objective-c) within a method that you would like to condense to a single call (for brevity or readability) what is the preferred practice.


  1. 不要这样做,将所有相关代码保留在方法中。

  2. 向有问题的对象添加新方法,即-(NSString *)formatedTime ;

  3. C样式的函数,在有关对象之外定义。

  4. 其他(即我错过的东西)。

  1. don't do it, keep all related code in the method.
  2. Add a new method to the object in question i.e. - (NSString *)formatedTime;
  3. C-Style functions, defined outside the object in question.
  4. Other i.e. something I missed.


推荐答案

重写代码时,我的主要原则是始终确保我编写的内容尽可能可读。考虑到这一点,我通常:

My primary rule when rewriting code is always to ensure whatever I write is as readable as possible. With that in mind, then, I usually:


  1. 仅在方法相对较短(适合屏幕或两者),并且在逻辑上都是相关的

  2. 如果新方法的名称将使其更具可读性,减少代码重复或使流程对读者更有意义,则添加一个新方法

  3. 除非处理非常低级的内容或在其他C样式函数的上下文中使用,否则避免使用C样式函数;基本上与您要重写的代码风格保持一致

  4. 在所有其他方面,请尝试确保一旦重写后该方法仍然有意义,那么它应该执行的操作(即没有错误)引入),并且可以被其他开发人员快速阅读和理解

  1. Keep all the code in the method only if it's relatively short (fits in a screen or two) and is all logically related
  2. Add a new method if the name of the new method will make it more readable, reduce duplication of code, or make the flow make more sense to a reader
  3. Avoid C-style functions unless dealing with something very low-level, or in the context of other C-style functions; basically keep with the style of the code you're rewriting
  4. Above all else, try to ensure that the method once rewritten still makes sense, does what it's supposed to do (i.e. no bugs were introduced), and can be quickly read and understood by another developer

这篇关于压缩代码的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆