`
experience
  • 浏览: 192366 次
社区版块
存档分类
最新评论
阅读更多

 

Small Methods: Nine Benefits of Making Your Methods Shorter

小函数:让你的函数变短的九个好处

译者注:本文英文版来自http://langrsoft.com/articles/smallMethods.shtml。翻译不改变原文的版权。中文版版权归本人所有。

I've espoused, on many occasions, making your methods short. The pattern Composed Method, defined by Kent Beck in Smalltalk Best Practice Patterns, says that methods should do things only at one level of abstraction. Methods should be short; a good size for Smalltalk methods is one to a half-dozen lines. That translates to about one to twelve lines (not counting braces!) for Java methods.

I recently engaged in a discussion at JavaRanch where someone thought "20 to 25 lines" was too short. After being taken aback, my response was that there are many reasons that your average method size should be considerably smaller. Here are nine benefits to short methods:

在很多场合我都表明过支持短小的函数。Kent Beck在Smalltalk Best Practice Patterns中定义的Composed Method模式指出函数应该只能做一个层次上的抽象。函数应该是短小的;对于Smalltalk函数大小应该在一至六行。翻译到Java应该是一至十二行(不包括大括号!)。

我最近在JavaRanch参加了一个讨论,那儿有些人认为“20到25行”太短。吃惊之余,我的回应是有很多原因让你的平均函数体积应该相当的小。下面是使用小函数的九个好处:

  1. Maintenance costs. The longer a method, the more it will take you to figure out what it does, and where your modifications need to go. With shorter methods, you can quickly pinpoint where a change needs to go (particularly if you are coding using test-driven development).
  2. Code readability. After the initial learning curve, smaller methods make it far easier to understand what a class does. They can also make it easier to follow code by eliminating the need for scrolling.
  3. Reuse potential. If you break down methods into smaller components, you can start to recognize common abstractions in your code. You can minimize the overall amount of code dramatically by reusing these common methods.
  4. Subclassing potential. The longer a method is, the more difficult it will be to create effective subclasses that use the method.
  5. Naming. It's easier to come up with appropriate names for smaller methods that do one thing.
  6. Performance profiling. If you have performance issues, a system with composed methods makes it easier to spot the performance bottlenecks.
  7. Flexibility. Smaller methods make it easier to refactor (and to recognize design flaws, such as feature envy).
  8. Coding quality. It's easier to spot dumb mistakes if you break larger methods into smaller ones.
  9. Comment minimization. While comments can be valuable, most are unnecessary and can be eliminated by prudent renaming and restructuring. Comments that restate what the code says are unnecessary.
  1. 维护成本。越长的函数越要花费更大的成本去了解它要做什么,以及在什么地方作修改。而对于小函数,你可以快速的查明应该在何处作修改(尤其是在应用测试驱动开发的时候)。
  2. 代码可读性。在初始的学习曲线之后,小函数使得你更容易理解一个类要做什么。而且你不必滑动窗口就可以理解代码。
  3. 拥有重用潜力。如果你把函数分解成一些小的模块,你可以识别出代码中通用的抽象。你可以通过使用这些通用的抽象使你的代码总量急剧下降。
  4. 拥有子类化潜力。函数越长,你就越难以创建一个子类使用这个函数。
  5. 命名。小的函数命名起来要容易一些,因为它做的事情少。
  6. 性能测度。如果你遇到了性能问题,一个使用composed method的系统更容易定位性能瓶颈。
  7. 灵活性。小函数使得重构更加容易(并且容易找到设计缺陷,比如feature envy)。
  8. 代码质量。如果把大函数分解成小函数,定位隐晦的错误更加容易。
  9. 最小化注释。虽然注释是很有价值的东西,大多数注释可以通过谨慎的命名和调整结构消除。重述代码已经说清楚的注释是没有必要的。

You should get the idea that most of the benefits are about improving the design of your system. Breaking methods up into smaller ones obviously leads to lots of smaller methods. You will find that not all of those methods should remain in the same class in which the original method was coded. The small methods will almost always point out to you that you are violating the basic rule of class design: the Single Responsibility Principle (SRP).

你应该已经发现多数的好处都是关于提升系统设计的。把函数分解成小函数当然会带来大量的小函数。你会发现并不是所有的这些小函数应该留在原来那一个类中。小函数几乎总是会指出你违反了类设计的基本规则:单一职责原则(the Single Responsibility Principle (SRP))。

The SRP states that a class should have only one reason to change. Put another way, a class should do one thing and one thing only. A class to present a user interface (UI) should do just that. It shouldn't act as a controller; it shouldn't retrieve data; it shouldn't open files; it shouldn't contain calculations or business logic. A UI class should interact with other small classes that do each of those things separately.

Break a class into small methods, and you will usually find all sorts of violations of the SRP.

SRP规定一个类只能因为一个原因去修改。换一种说法,一个类应该做并且只做一件事情。一个呈现用户接口(UI)的类,它就只能做它要呈现的这些事情,而不应当作为控制器,或者接收数据;或者打开文件或者包含计算和业务逻辑。一个UI类应该与其他的小类交互而有那些小类分别去完成那些事情。把一个类分解成很多的小函数,你通常会发现各种各样的对SRP的违反。

Initially, you may find it more difficult to work with code with lots of methods. There is certainly a learning curve associated with doing so. You will find that the smart navigational features of your IDE (for example, Eclipse) can go a long way toward helping you understand how all the little methods fit together. In short time, you will find that well-composed code imparts much greater clarity to your system.

One oft-repeated resistance to short methods is that it can degrade performance in your system. Indeed, method calls are usually fairly expensive operations. However, you rarely create performance problems with more methods. Poor performance can usually be attributed to other factors, such as IO operations, network latency, poor choice of algorithm, and other inefficient uses of resources.

开始,你可能会发现使用大量小函数更加困难。这只是学习曲线的问题。你会发现IDE(比如Eclipse)的智能导航特性会对你的理解这些小函数如何组合在一起大有帮助。短期,你会发现这样的代码给予系统更好的清晰度。

经常遇到的对于小函数的抵抗是它会降低系统性能。确实,方法调用通常是比较昂贵的操作。然而,你极少有机会因为更多的函数制造性能问题。低性能通常是由其他因素造成的,比如IO操作,网络响应,差算法,以及其他的低效的资源使用。

Even if additional method calls do noticeably impact performance (performance is not an issue until someone recognizes it as one), it's very easy to inline methods. The rule of performance is always: make it run, make it right (e.g. small methods), make it fast (optimize).

甚至即使多余的方法调用对性能产生了显著的影响(如果没有人注意到性能就不是问题),inline method是非常容易的。关于性能的规则总是:先让它跑起来,然后让它正确(比如小函数),然后让它快起来(优化)。

While it's difficult to do, you can go too far and create too many methods. Make sure each method has a valid reason for existing; otherwise inline it. A method is useless if the name of the method and the body of the method say exactly the same thing.

There are exceptions to every rule. There will always be the need for the rare method larger than 20 lines. One way to look at things, however, is to look at smaller methods as a goal to strive for, not a hard number. Instead of debating whether 25 lines, 12 lines or 200 lines is acceptable, see what you can do to reduce method length in code that you would've otherwise not touched. You'll be surprised at how much code you can eliminate.

虽然本来这做起来并不容易,你可能会做得太过火并且创建过的函数。确信每个函数有一个恰当的存在的原因;否则inline之。如果一个函数的body已经清楚的表明它做什么这个函数就没有必要单独提出来。任何规则都有特例。总是有时候极个别函数需要超过20行。看问题的角度是,把小函数看作一个奋斗的目标,而不是一个固定数字。与其争论25行,12行或者200行那个更可以接受,倒不如看看你可以做点什么来减少那些你不愿意碰的函数的行数。你会吃惊的发现你可以消除多少代码。

Few people promote pushing in the other direction. Increasing the average method size represents sheer carelessness or laziness. Take the time and care to craft your code into a better design. Small methods!

很少人支持相反的观点。增加平均函数大小完全是不小心或者懒惰。花点时间修理一下代码改善设计。小函数万岁!

分享到:
评论

相关推荐

    超级实用的抓牛专用绝无未来函数通达信指标公式源码.doc

    超级实用的抓牛专用绝无未来函数通达信指标公式源码.doc

    Python函数知识点(详解)

    如果在开发程序时,需要某块代码多次,但是为了提高编写的效率以及代码的重用,所以把具有独立功能的代码块组织为一个小模块,这就是函数 定义函数的格式如下: def 函数名(): 代码 Python 使用def 开始函数定义,...

    QT 绘图函数

    因为反走样很难让你控制到1个像素。这不是 Photoshop画笔工具的缺陷,而是反走样算法的问题。如果你想了解为什么这样,请查阅计算机图形学里面关于反走样的原理部分。 反走样是图形学中的重要概念,用以防止...

    jQueryTest.rar

    这里是个简单的表单验证...简单的传递一个函数给$()函数:3.jQuery让Ajax变得异常简单。使用jQuery,Ajax恐怕不能变得再简单了. jQuery有一系列的函数,可以使简单的事情变得真正简单,让复杂的事情也能变得尽可能的简单.

    C_gjz.rar_float

    C语言中的关键字 auto :声明自动变量 一般不使用 double :声明双精度变量或函数 float:声明浮点型变量或函数 int: 声明整型变量或函数 short :声明短整型变量或函数 long :声明长整型变量或函数 struct:声明...

    使用复数调用二维实函数 (cmplx.m):一个包装函数,允许在 F(X,Y,...) 形式的函数中使用复数。-matlab开发

    我经常将 X,Y 矢量数据存储为单个复矢量 V = X + iY。 这使得平移、旋转和缩放操作变得容易。 在复平面中,数据点与点积和叉积之间的距离也很容易。 对于需要 F(X,Y,...) ... 这是一个非常短的函数,但我希望你觉得

    PHP封装的字符串加密解密函数

    函数名称:encrypt 函数作用:加密解密字符串 使用方法: 加密 :encrypt('str','E','nowamagic'); 解密 :encrypt('被加密过的字符串','D','nowamagic'); 参数说明: $string :需要加密解密的字符串 $operation:...

    OOP_Crash_Course_Cpp:它包含用于快速修改C ++中的OOP的材料

    让我们以最简单,最快的方式进行C ++ OOP概念速成班,并准备在短时间内进行采访:man_superhero_light_skin_tone: 主题名称 新刊登位置 :flashlight:覆盖新的&删除的 :flashlight:这个指针 :flashlight...

    易千明模块2.0 易语言模块

    ' 增加了汇编_读短整型内存 函数 ' 增加了汇编_读短整型内存 函数 ' 增加了 小数变量取地址 ' 文本变量取地址 函数 ' 字节集变量取地址 函数 ' 整数型变量取地址 函数 ' 新增粉碎文件 函数 ' 增加了输入法注入类下的...

    sm3国密算法的生日攻击(C++实现)

    这里我使用了类似查表攻击似的数据结构,一边存表一边查表(可以使用多线程进一步优化脚本性能),以便可以在较短时间内找到一个前16bit的hash弱碰撞。 如果寻找更长bit的碰撞,寻找时间也会相应变长。 运行指导 ...

    智能卡的散列函数

     一般而言,单向散列函数是从可变长度的文档导出固定长度值的函数,这个值以压缩的形式代表了文档原来的内容,并且不能用来重建原来的文件。在智能卡领域,这些函数被排它地用来计算数字签名的输人值。如果文件的...

    k:R的“ K”-所有内置函数都非常短!

    R的 -所有内置函数都非常短! 使您的R生活变得更简单,更快捷: if ( ! require( K.R )) devtools :: install_github( ' robertzk/k ' ) library( K.R ) a( iris [ 1 : 4 ], 1 , sum ) # apply b() # browser cc( ...

    论文研究-多峰函数优化的自适应小生境克隆选择算法.pdf

    为了解决de Castro在2000年提出的CLONALG算法在多峰值函数优化时多峰搜索能力弱,训练时间长的问题,提出自适应小生境克隆选择算法(ANCSA)。该算法运用自适应小生镜技术、高频变异算子和小生镜免疫优势选择技术来...

    python中实现php的var_dump函数功能

    php中var_dump是一个特别有用的函数,它可以输出任何变量的值,不管你是一个对象还是一个数组,或者只是一个数。它总能用友好的方式输出,我调试的时候经常会需要看某位置的变量信息,调用它就很方便: 但是开发...

    论文研究-基于小波的非参数回归模型均值变点的Bootstrap监测.pdf

    论文研究-基于小波的非参数回归模型均值变点的Bootstrap监测.pdf, 本文首次考虑非参数回归模型均值函数结构变点的在线监测问题. 首先对回归函数的局部线性估计值进行...

    矩形棒(不同热源)中的稳态温度:此函数计算多层矩形棒横截面中的 T (z,y)。 边缘发射激光器的纵向截面。-matlab开发

    这个函数是我之前上传的 T_rect_rod 的变体。 区别在于热源。 现在它的范围从一个侧壁到另一个侧壁,由三段组成:中间的长段 (ga) 和杆侧壁附近的短段 (gm)。 最初,该问题是在边缘发射几何形状的半导体激光器中的...

    CodeIgniter:php敏捷开发框架web快速开发详解

    CI 给你一个简单的函数,可以这样编写超链接: 复制代码到剪贴板PHP 代码echo anchor('start/hello/fred', 'Say hello to Fred');CI 推荐你把你的 URL 放入一个配置文件中供你的脚本读取。CI 的 anchor 函数会自动...

    powerbuilder

    参数printjobnumber:用PrintOpen()函数打开的打印作业号fontnumber:指定赋给当前定义字体的编号,有效值在1到8之间 facename:string类型,指定字体名称,该字体应该是你的打印机支持的字体,比如“宋体”height:...

    C语言入门经典(第4版)--源代码及课后练习答案

    11.3.2 结构指针作为函数变元 425 11.3.3 作为函数返回值的结构 426 11.3.4 修改程序 430 11.3.5 二叉树 433 11.4 共享内存 442 11.4.1 联合 442 11.4.2 联合指针 444 11.4.3 联合的初始化 444 11.4.4 联合...

    混沌时间序列的小波神经网络预测方法及其优化研究(高清)

    (8)用全局优化方法——填充函数法研究了小 波神经网络 的优化方法,构造了一种新的易于计算的单参数的填 充函数,不 仅证明了新构造的函数具有填充函数的性质,还把填 充函数和 BP算法相结合,提出一种训练小波...

Global site tag (gtag.js) - Google Analytics