函数/过程/方法应该有多少行代码? [英] How many lines of code should a function/procedure/method have?

查看:197
本文介绍了函数/过程/方法应该有多少行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

何时是功能太长?

最近被赋予了审查由另一个开发人员编写的不良代码并记录不良做法的不可取的任务。 (这是所有为了退出支付开发商的工作,而不是任何利他的理由,当然!)

I've recently been given the unenviable task of reviewing poor code written by another developer and documenting the bad practices. (This is all for the purposes of getting out of paying for the developer's work rather than any altruistic reason, of course!)

审查的代码有几个程序是很多代码行 - 最长的几乎是600行。我想到的几个问题是可维护性和可读性。

The reviewed code has several procedures that are many lines of code - the longest is almost 600 lines. A couple of problems with this that I've thought of are maintainability and readability.

诀窍是我需要向外行人证明为什么这是一个坏的做法,如果可能的话,可以用一本备受好评和最新的参考书。

The trick is that I need to justify to a layperson why this is a bad practice and if possible back it up with a well regarded and current reference book. Analogies are good too.

任何想法?

重复 =http://stackoverflow.com/questions/475675/when-is-a-function-too-long>什么时候函数太长?

重复: 最大功能尺寸的最佳规则?

Duplicate: When is a function too long?
Duplicate: Best rule for maximum function size?

推荐答案

这不是关于代码行。如 Steve Mcconnell Bob Martin (关于编码最佳实践的两个很好的参考),一个方法应该做一件事,只做一件事。然而许多行代码需要做一件事是它应该有多少行。如果一件事可以分成更小的事物,那么每个事物都应该有一个方法。

It's not about lines of code. As Steve Mcconnell and Bob Martin say (two pretty good references on coding best practices), a method should do one thing and only one thing. However many lines of code it takes to do that one thing is how many lines it should have. If that "one thing" can be broken into smaller things, each of those should have a method.

好的线索你的方法做了多个事情:

Good clues your method is doing more than one thing:


  • 方法中有多个缩进级别(表示太多逻辑分支​​只能做一件事)

  • Paragraph Breaks - 逻辑代码组之间的空格表示方法正在做多个事情

。鲍勃·马丁也说,保持在10左右。我个人通常尝试拍摄10.如果它开始接近20,这是一个精神上的标志,以更加注意这种方法。但最终,LoC是几乎任何东西的糟糕指标。这只是一个有用的指标,可能指向真正的问题。

Just to name a few. Bob Martin also says to keep it around 10. Personally I usually try to shoot for 10. If it starts getting close to 20, that's a mental flag to pay closer attention to that method. But ultimately, LoC is a bad metric for pretty much anything. It is only a helpful indicator that can potentially point to the real issue.

这篇关于函数/过程/方法应该有多少行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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