函数/过程/方法应该有多少行代码?

发布于 2024-07-14 22:43:51 字数 520 浏览 19 评论 0 原文

可能的重复:
什么时候函数太长?

我最近接到了一项令人羡慕的任务审查其他开发人员编写的不良代码并记录不良实践。 (当然,这都是为了摆脱开发人员的工作报酬,而不是出于任何利他的原因!)

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

诀窍在于,我需要向外行证明为什么这是一种不好的做法,并且如果可能的话,用一本备受推崇的最新参考书来支持它。 类比也很好。

有任何想法吗?

重复: 函数何时太长?

Possible Duplicate:
When is a function too long?

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!)

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.

Any ideas?

Duplicate: When is a function too long?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

柳絮泡泡 2024-07-21 22:43:51

这与代码行无关。 正如 Steve McConnellBob Martin 说(关于编码最佳实践的两篇非常好的参考文献),一个方法应该做一件事,而且只能做一件事。 不管需要多少行代码来完成这一件事,重要的是它应该有多少行。 如果“一件事”可以分解成更小的事情,那么每个事情都应该有一个方法。

好的线索表明您的方法正在执行不止一件事:

  • 方法中的缩进超过一级(表示逻辑分支过多而只能执行一件事)
  • “段落分隔符”-逻辑代码组之间的空格表示该方法正在执行更多操作不仅仅是一件事

仅举几例。 Bob Martin 还说将其保持在 10 左右。就我个人而言,我通常会尝试争取 10。如果它开始接近 20,则表明需要更加关注该方法。 但归根结底,代码行数对于几乎任何事情来说都是一个不好的衡量标准。 它只是一个有用的指标,有可能指出真正的问题。

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:

  • More than one level of indention in a method (indicates too many logic branches to only be doing one thing)
  • "Paragraph Breaks" - whitespace between logical groups of code indicate the method is doing more than one thing

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, lines of code are a bad metric for pretty much anything. It is only a helpful indicator that can potentially point to the real issue.

枕梦 2024-07-21 22:43:51

真正的答案

没有具体的数字。

具体答案

如果您必须向律师或其他人证明某个数字的合理性,请计算出适合您商店中典型开发编辑器窗口的最大行数,然后使用它。

一般实践

您甚至不应该真正这样看待它,但任何一个函数中都不应该发生任何非常复杂的事情。

每个工作单元都应该委托给其自己的单元可测试的描述性命名方法。 这样做,你所有的方法都会变得很小并且可读,而无需计算行数......

我看到的最大的问题是 3-4+ 布尔条件在 if 语句中间爆炸。 将所有这些内容包装在一个具有好名称的布尔值中,然后将组成它的任何本身复杂的部分包装起来。

The Real Answer

There's no specific number.

A Concrete Answer

If you have to justify with some number to lawyers or something, figure out the maximum number of lines that fit on a typical development editor window at your shop, and use that.

General Practice

You shouldn't even really look at it that way, but there should be nothing very complex going on in any one function.

Each unit of work should be delegated to its own unit testable descriptively named method. Do this and all your methods end up tiny and readable without ever counting lines......

The biggest offender I see is 3-4+ boolean conditions exploded in the middle of an if-statement. Wrap all that up in one boolean with a good name, then wrap up any pieces that make it up that are complex in their own.

凉宸 2024-07-21 22:43:51

首先,请注意,长度限制与通常的度量完全分开,通常的度量是“函数是否只做一件事,并且做得很好?” 如果这个问题的答案是否定的,那么无论长度如何,该函数都可能不是一个好的函数。

特别与最大长度相关的是《Code Complete》中的一段话,该书通常被认为是关于编码实践主题的最佳书籍之一:

有时,复杂的算法会导致例程更长,在这种情况下,应该允许例程有机增长至 100-200 行。 (一行是源代码中的非注释、非空行。)数十年的证据表明,如此长度的例程并不比较短的例程更容易出错。 让诸如嵌套深度、变量数量和其他与复杂性相关的考虑因素等问题决定例程的长度,而不是强加长度限制本身。

如果您想编写超过 200 行的例程,请小心。 没有任何研究报告使用更大的例程来降低成本、降低错误率或两者兼而有之,以区分大于 200 行的大小,并且当您传递 200 行代码时,您必然会遇到可理解性的上限。

First of all, note that the length restriction is entirely separate from the usual metric, which is "does the function do only one thing, and do it well?" If the answer to that question isn't yes, the function is probably not a good one anyway, regardless of length.

Relevant specifically to the maximum length, a quote from Code Complete, generally considered to be one of the best books on the subject of coding practices:

From time to time, a complex algorithm will lead to a longer routine, and in those circumstances, the routine should be allowed to grow organically up to 100-200 lines. (A line is a noncomment, nonblank line of source code.) Decades of evidence say that routines of such length are no more error prone than shorter routines. Let issues such as depth of nesting, number of variables, and other complexity-related considerations dictate the length of the routine rather than imposing a length restriction per se.

If you want to write routines longer than about 200 lines, be careful. None of the studies that reported decreased cost, decreased error rates, or both with larger routines distinguished among sizes larger than 200 lines, and you’re bound to run into an upper limit of understandability as you pass 200 lines of code.

伴我老 2024-07-21 22:43:51

尽可能少。

As few as possible.

半岛未凉 2024-07-21 22:43:51

为了补充雷克斯的观点,它也应该尽可能短。 鲍勃·马丁说 10 个或更少

对象导师 - 函数应该有多大?

To add to Rex's point, it should also be as short as possible. Bob Martin says 10 or fewer

Object Mentor - How big should a function be?

小嗷兮 2024-07-21 22:43:51

我已经很多年没有读过这篇文章了,但我认为在学习 Perl 中,他们建议制作一个过程的长度不要超过你可以一次在屏幕上显示整个内容的长度。 我认为这是一个很好的衡量标准。 我见过较长的函数由于重复的代码(例如数据库访问和分配属性值)而仍然可读,但这些都是例外而不是常态。

It's been many years since I read this, but I think it was in Learning Perl that they recommend making a procedure no longer than you can fit the whole thing on the screen at once. I thought this was a good yardstick. I've seen longer functions that were still readable because of repetitive code (e.g. database access and assigning property values), but those are the exception rather than the norm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文