打破“为”使用“break”循环被认为有害?

发布于 2024-09-08 17:40:25 字数 482 浏览 12 评论 0原文

前几天我开始了一个快速的开源项目,当一些朋友在svn上查看代码时,其中一个告诉我,在for循环中使用break语句是被认为是有害的,不应该这样做。

不过,他补充说,我会在 Linux 内核源代码的 for 循环中发现一些 break 语句的情况,但这只是因为只允许 Linus Torvalds 和 Chuck Norris使用它而不是其他人。

你怎么认为?我认为在 for 循环中使用 break 没有问题。在我看来,使用布尔变量或类似的东西来模拟 break 的行为会增加很多不必要的开销,并使代码变得不那么简单。

另外,没有与 goto 进行比较的余地,因为 break 不能像 goto 那样任意地将程序的流程从一个点更改为另一个点。

Some days ago I started a quick open source project and, when some mates looked at the code on svn, one of them told me that using break statement inside a for loop is considered harmful and shouldn't be done.

He added, though, that I would find several cases of break statements inside for loops on Linux kernel source code, but that was just because only Linus Torvalds and Chuck Norris were allowed to use it and no one else.

What do you think? I see no problem in using break inside a for loop. In my opinion, emulating the behaviour of break using boolean variables or something alike adds a lot of innecesary overhead and makes the code less straightforward.

Also, there's no room for comparison with goto, because break cannot arbitrarily change program's flow from one point to the other lie goto does.

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

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

发布评论

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

评论(9

于我来说 2024-09-15 17:40:25

我认为使用休息时间没有问题。总会有这样的情况,您想要停止处理循环,并且使用 break; 比将循环计数器设置为一个值更有意义(并且使其更具可读性!)你的循环在下一次迭代时停止。

I see no problem with using breaks. There will always be circumstances where you want to stop processing a loop, and using a break; makes much more sense (and makes it more readable!) than setting your loop counter up to a value that would make your loop stop at the next iteration.

红衣飘飘貌似仙 2024-09-15 17:40:25

强制:

XKCD 转到

要点是,您不应纯粹因为不良做法(或迅猛龙)而避免使用它,而应根据具体情况进行考虑。

这一切都与清晰度有关。正如您所说,您永远不必使用它,但在某些情况下它可以提高可读性。当循环通常正常终止但​​在极少数情况下您必须跳出时,它很有用。通常(或总是)中断的循环更多的是代码味道(但仍然可能是合适的)。

Obligatory:

XKCD Goto

The point is that you should not avoid it purely on grounds of bad practice (or velociraptors), but consider on a case by case basis.

It's all about clarity. As you said, you never have to use it, but in some cases it promotes readability. It's useful when the loop usually terminates normally, but in rare cases you have to bail out. Loops that usually (or always) break are more of a code smell (but could still be appropriate).

深爱不及久伴 2024-09-15 17:40:25

使用 break 不仅没有问题,我想说任何说它“被认为有害”的人都是完全错误的。

break 是一种用于中止循环的语言功能 - 您可以使用 goto,但随后您会招致下面 XKCD 漫画的(适当的)愤怒。您可以在条件中使用标志,但这会妨碍可读性。 break 不仅是最简单的,而且也是最清晰的跳出循环的方法。按其应有的用途使用它。


编辑:要了解更大的情况:当您编写代码时,“我应该使用语言功能 X 还是 Y”的指导原则应该是“哪种方式会产生更优雅的代码” “?代码的优雅几乎是一门艺术,但我认为它是可读性和算法(而不是微优化)效率之间的良好平衡。可读性将由代码的长度、复杂性等决定。一行 boost::bind 很可能更难阅读和理解。理解比 3 行循环。

如果某种语言功能可以帮助您在完成工作的同时编写更易于理解的代码,那么就使用它。这适用于 breakgoto、C++ 异常等。不要盲目遵循“X 是(邪恶|被认为有害)” - 每次都应用常识和逻辑。

Not only is there no problem to using break, I'd say anyone saying it is "considered harmful" is downright wrong.

break is a language feature used to abort a loop - you could use goto, but then you incur the (appropriate) wrath of the XKCD comic below. You could use a flag in the condition, but that hampers readability. break is not only the easiest, but also the clearest way many times to bust out of a loop. Use it as it was meant to be used.


Edit: To get at a larger picture here: When you're writing code, the guiding principle to "should I use language feature X or Y" should be "which way will result in the more elegant code"? Elegance, in code, is pretty much an art, but I'd lay it down as a fine balance between readability and algorithmic (read: not micro-optimizations) efficiency. Readability is going to be determined by length, complexity of the code, etc. A one-line boost::bind may very well be harder to read & understand than a 3 line loop.

If a language feature can help you write code that is easier to understand while getting the job done, then use it. This applies to break, goto, C++ exceptions, etc. Don't follow a "X is (evil|considered harmful)" blindly - apply common sense and logic each time.

筱果果 2024-09-15 17:40:25

break不仅没有问题,而且break不够时,可以使用goto。也不要害怕多重回报。

以上所有内容仅在使代码更易于理解的情况下才适用*。

*并且如果您的 PHB 允许...

Not only is there no problem with break, it's also OK to use goto when break is insufficient. Don't be afraid of multiple returns, either.

All of the above only applies if it makes the code easier to understand*.

*And if your PHB allows it...

天暗了我发光 2024-09-15 17:40:25

我认为你的伴侣是个疯子。 break 是完全可以接受的,完全可读的,并且完全可维护的。时期。

I think your mate is insane. break is perfectly acceptable, perfectly readable, and perfectly maintainable. Period.

我的黑色迷你裙 2024-09-15 17:40:25

有一种范例是任何循环都应该只有一个退出点(就像函数应该只有一个返回一样)。这与可读性有关。太多的退出点会使代码非常难以理解。另外,如果您想要进行代码验证(即数学证明您的代码是否正确),这一点也很重要。

然而,指南通常可以提供帮助,但并不严格。在某些情况下,休息可能比不休息更好。因此,人们应该对此保持务实的态度,但要理解这些原则的原因,以便创建好的代码。

There is a paradigm that any loop should only have one point of exit (same as a function should only have one return). This has to do with readability. Too many exit points can make the code very difficult to understand. Also, it is important if you want to do code verification (i.e. mathematically proof if your code is correct).

However, guidelines are often there to help, but are not strict. There are possibly situations where a break is better than not using it. Hence, one should be pragmatic about this, but understand the reason for such principles in order to create good code.

双手揣兜 2024-09-15 17:40:25

增加循环计数器而不是使用break也意味着您完成了循环的当前迭代的执行,这可能是也可能不是所需的。
当然,您可以将其余部分包装在 if 子句中,然后当您意识到需要多次检查是否停止循环时再次执行,您很快就会意识到为什么人们使用中断;

Incrementing your loop counter instead of using break also means you finish executing the current iteration of the loop, which may or may not be desirable.
Of course, you could wrap the rest of it in an if clause, and then do it again when you realize you need to check whether or not to stop looping multiple times, and you will quickly realize why people use break;

怪我鬧 2024-09-15 17:40:25

如果您正在考虑使用给定的技术,我建议使用此算法。

  • 如果它被认为是良好实践
    • 使用它。
  • 如果它被认为是好的做法:
    • 仅当您认为它是最佳的长期解决方案时才使用它。
  • 如果它被认为是邪恶
    • 仅当您认为它是最佳的长期解决方案并且您对自己的判断非常有信心时才使用它。请注意:被视为邪恶的事物往往具有欺骗性的吸引力。

我将 break; 归类为“不是好的做法”。当它使代码更具可读性、减少出现错误的机会、不会使调试复杂化等时使用它。

I suggest this algorithm if you're considering using a given technique.

  • If it is considered good practice:
    • use it.
  • If it is not considered good practice:
    • use it only if you judge it to be the best long-term solution.
  • If it is considered evil:
    • use it only if you judge it to be the best long-term solution and you are extremely confident in your judgement. Beware: things which are considered evil tend to be deceptively appealing.

I would classify break; as "not good practice". Use it when it makes the code more readable, reduces the chance of bugs, does not complicate debugging, etc.

梦旅人picnic 2024-09-15 17:40:25

我认为这取决于具体情况。虽然在某些情况下它可能是“坏”的编码风格,但我想不出它会有害的情况。

事实上,在某些情况下我会推荐它。例如,如果您使用线性搜索,任何情况(除了最坏的情况),break都会提高您的速度。我认为在以下情况下打破循环:您发现您的针是完全可以接受的,并且它比乱搞循环变量(这可能不仅仅用于循环,具体取决于您的程序)或将循环体包装在 中更具可读性if 块。 (另一个选项,包含 continueif,结合了两个世界中最糟糕的情况:将循环逻辑包装在 if 块中,并且像你的朋友这样不喜欢 break 的人会抱怨糟糕的编码风格。)

I think it depends on context. While it may be 'bad' coding style in some situations, I can't think of a case where it would be harmful.

In fact, in some cases I would recommend it. For example, if you were using a linear search, any case (except the very worst case), a break would improve your speed. I think breaking out of the loop when you've found your needle would be perfectly acceptable, and it would be more readable than messing around with the loop variable (which might not solely be used for the loop, depending on your program) or wrapping the loop body in an if block. (And the other option, an if which contains continue, combines the worst of two worlds: wrapping loop logic in an if block, and the poor coding style railed against by people like your friends who don't like break.)

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