简体   繁体   English

方法重构

[英]Method refactoring

在您需要更好地设计之前,方法可以使用多少行代码进行良好的设计?

Lines of code is an incomplete metric to use for refactoring. 代码行是用于重构的不完整度量。 Cyclomatic Complexity should be used in conjuction with LOC as a decent gauge for when to refactor. Cyclomatic Complexity应与LOC结合使用,作为何时重构的合适标准。

A decent guideline is 一个体面的指导方针是

LOC > 80 || CC > 10 == "Time to refactor"

Chances are you will hit a Cyclomatic Complexity > 10 long before you ever reach 80 lines of code. 在你达到80行代码之前,你很可能会遇到一个> 10的Cyclomatic Complexity。

Of course there are a litany of other metrics to consider as well: 当然,还有一些其他指标需要考虑:

  1. Afferent Coupling: How many methods depend on this method 传入耦合:有多少方法依赖于此方法
  2. Efferent Coupling: How many methods does this method rely on 传出耦合:此方法依赖多少种方法
  3. No. of Parameters 参数数量
  4. No. of Varialbes used 使用的Varialbes数量

You can go pretty in depth, but all this to say that determining when to refactor can't be decided with a blanket rule like "Thou shalt not have more than 40 lines in any method!" 你可以深入探讨,但所有这一切都说明,确定何时重构不能用一条全面的规则决定,比如“你不能用任何方法超过40行!”

There is no accepted rule. 没有被接受的规则。 It should be as simple to read as possible. 它应该尽可能简单易读。 If that means taking 5 lines of a 10 line block and putting them in their own method, then so be it. 如果这意味着占用10行的5行并将它们放入自己的方法中,那就这样吧。 But sometimes it can be fine to have 50-line methods. 但有时候,使用50行方法可能会很好。 Just do what you think you'll understand best in 6 months time. 在6个月的时间里做你认为最能理解的事情。

Depends on what the method does. 取决于该方法的作用。 There's a fine line between writing too few methods and too many methods, and much of it is a matter of personal taste. 在编写太少的方法和太多的方法之间有一个很好的界限,其中大部分都是个人品味的问题。

My opinion: 我的意见:

  • 50 lines is too long for a method. 对于一种方法,50行太长。 I think methods shorter than this are often too long as well, but that's my highest threshold for method length. 我认为比这更短的方法通常也太长,但这是我方法长度的最高阈值。
  • Methods should be written so that they're used more than once. 应编写方法,使其不止一次使用。 Don't refactor just to make an existing method shorter; 不要仅仅为了缩短现有方法而进行重构; refactor to make reusable methods that other methods will benefit from. 重构以制作其他方法将受益的可重用方法。

There are no arbitrary limits on the length of code a method can have before it needs to be redesigned. 在需要重新设计方法之前,方法可以拥有的代码长度没有任意限制。 However, usually if a method feels "too long", it's a sign that you're doing something wrong. 但是,通常情况下,如果方法感觉“太长”,则表明您做错了。

Not as a rule, but as a heuristic, when a method goes over 25 lines I give it a once over to see if I can break it into simpler components. 这不是一个规则,但作为一种启发式方法,当一个方法超过25行时,我给它一次,看看我是否可以将它分解为更简单的组件。

This is not to say that methods over 25 lines are never well designed, or that shorter methods are always better than longer ones. 这并不是说超过25行的方法从来没有很好地设计,或者较短的方法总是比较长的方法更好。 It's just a good heuristic to follow. 这只是一个很好的启发式方法。

As many as it takes to logically and clearly perform the one single task that method performs. 尽可能多地逻辑和清楚地执行方法执行的单个任务。

Seriously, there is no yardstick. 说真的,没有标准。 Generally people like to be able to see an entire method within one page on their monitor, so that's one thing to consider. 通常人们喜欢能够在他们的显示器上的一个页面内看到整个方法,因此这是一件需要考虑的事情。 But line count is not your primary indicator of logic problems. 但行数并不是逻辑问题的主要指标。 Too many developers have obfuscated code and made future support more difficult by trying to get it into as few lines as possible. 太多的开发人员对代码进行了模糊处理,并且通过尝试将代码尽可能少地编入代码来使未来的支持变得更加困难。

There's nothing inherently wrong with 1,000 lines of code in a single method, if that's what it takes to logically and clearly perform that task. 如果这是逻辑上和清楚地执行该任务所需要的,那么单个方法中的1000行代码就没有任何内在错误。 It's rare, and a method that long is usually a sign to re-factor, but it's possible. 这种情况很少见,长期通常是重新考虑因素的方法,但这种方法是可能的。 There is something inherently wrong with 1,000 lines of spaghetti code all doing different things and interacting in strange ways in a single method. 也有一些内在的错误1000行的面条代码都在做不同的事情,在一个单一的方法奇特的方式进行交互。 The line count isn't the problem. 行数不是问题。

Probably, we should consider "Responsibility" implemented by a method here rather than go by hard numbers. 也许,我们应该考虑通过这里的方法实现“责任”,而不是通过硬数字来实现。 If we try to limit a method to a single responsibility, LOC will be taken care automatically. 如果我们尝试将方法限制为单一责任,LOC将自动处理。

Another rule of thumb that we can follow is to limit the length of a method that we can see on a single screen. 我们可以遵循的另一个经验法则是限制我们可以在单个屏幕上看到的方法的长度。

Lines of code is one metric, but another to look at is code paths. 代码行是一个度量标准,而另一个代码是代码路径。 Each if, and, or, loop, etc. makes the function more complex. 每个if,和,或循环等使函数更复杂。 You can have very short, complex functions and it's possible to have simple to understand, but long, functions. 您可以拥有非常简短的复杂功能,并且可以使用简单易懂的功能。 Refactoring works well when you use it reduce complexity, not necessarily lines of code. 使用它时,重构效果很好,降低了复杂性,而不一定是代码行。

For example: 例如:

if (color.r == color.g && color.r == color.b)
    if (color.r > 128)
       newColor = color.White;
    else
       newColor = color.Black;

vs VS

if(isGray(color))
   newColor = thresholdGray(color)

The intent is clearer, and the number of paths is reduced. 意图更清晰,路径数量减少。

It's true that LOC is a lousy metric, because it doesn't correlate well with the property you are really interested in (quality of design). 确实,LOC是一个糟糕的指标,因为它与你真正感兴趣的属性(设计质量)没有很好的相关性。 But its great strength as a metric - and the reason it is so widely used and discussed - is of course how easy it is to measure. 但它作为衡量指标的巨大优势 - 以及如此广泛使用和讨论的原因 - 当然是多么容易衡量。 That's not pure laziness; 那不是纯粹的懒惰; for a fraction of one percent of the effort, you get - well, maybe 20 or 30 percent of the value of more meaningful metrics. 只需百分之一的努力,你就可以得到 - 好吧,可能是更有意义的指标价值的20%或30%。 You can just glance at a method, or the last line of a class, and assess the LOC metric. 您只需浏览一个方法或类的最后一行,然后评估LOC指标。 You can just look at the height of the scroll bar and have a pretty good idea. 你可以看看滚动条的高度,并有一个非常好的主意。

And there are real LOC limits that make sense - too many to fit on a monitor, or on a page of text, is really too many, notwithstanding the simplicity and cohesiveness. 并且有真正的LOC限制是有意义的 - 太多而无法放在显示器上或文本页面上,实际上太多了,尽管简单和凝聚力。

I like really short methods. 我喜欢很简短的方法。 One-liners are great. 单行很棒。 When a method starts to head into double digit LOC, it itches. 当一个方法开始进入两位数LOC时,它会发出痒声。 If it's 30 or more, it's painful. 如果它是30或更多,那很痛苦。 Go ahead and use LOC; 继续使用LOC; just understand its weakness as a metric, and know what better metrics (eg cyclomatic complexity) exist. 只是将其弱点理解为度量标准,并了解存在哪些更好的指标(例如圈复杂度)。 Use them when you need something better. 当你需要更好的东西时使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM