继续认为有害?
Should developers avoid using continue in C# or its equivalent in other languages to force the next iteration of a loop? Would arguments for or against overlap with arguments about Goto?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
继续
对我来说感觉不对。break
让你离开那里,但continue
似乎只是意大利面条。另一方面,您可以使用
break
来模拟continue
(至少在 Java 中)。(这篇文章在上述代码中存在十多年来的明显错误。这对于
break
/continue
来说看起来不太好。)continue
在某些情况下可能很有用,但我仍然觉得很脏。 也许是时候引入一种新方法了。这些用途应该非常罕见。
continue
feels wrong to me.break
gets you out of there, butcontinue
seems just to be spaghetti.On the other hand, you can emulate
continue
withbreak
(at least in Java).(This posting had an obvious bug in the above code for over a decade. That doesn't look good for
break
/continue
.)continue
can be useful in some circumstances, but it still feels dirty to me. It might be time to introduce a new method.These uses should be very rare.
在大多数语言中,Continue 是一个非常有用的函数,因为它允许在某些条件下跳过代码块。
一种替代方法是在 if 语句中使用布尔变量,但每次使用后都需要重置这些变量。
Continue is a really useful function in most languages, because it allows blocks of code to be skipped for certain conditions.
One alternative would be to uses boolean variables in if statements, but these would need to be reset after every use.
我会说:“这取决于”。
如果您有相当小的循环代码(您可以在不滚动的情况下看到整个循环代码),通常可以使用 continue。
但是,如果循环体很大(例如由于一个大开关),并且有一些后续代码(例如在开关下面),则您可能会通过添加 continue 来轻松引入错误,从而有时会跳过该代码。 我在字节码解释器的核心遇到过这种情况,其中一些检测代码有时由于某些情况分支中的继续而未执行。
这可能是一个有点人为构造的情况,但我通常会尽量避免继续并使用 if (但不要像 Rob 的示例代码中那样嵌套太深)。
I'd say: "it depends".
If you have reasonably small loop code (where you can see the whole loop-code without scrolling) its usually ok to use a continue.
However, if the loops body is large (for example due to a big switch), and there is some followup code (say below the switch), you may easily introduce bugs by adding a continue and thus skipping over that code sometimes. I have encountered this in the heart of a bytecode interpreter, where some instrumentation code was sometimes not executed due to a continue in some case-branches.
This might be a somewhat artificially constructed case, but I generally try to avoid continue and use an if (but not nesting too deep as in the Rob's sample code).
我认为 continue 不会像 goto 一样困难,因为 continue 永远不会将执行移出它所在的代码块。
I don't think continue could ever be as difficult as goto since continue never moves execution out of the code block that it is in.
如果您正在迭代任何类型的结果集,并对所述结果执行操作,例如在每个结果中,并且如果一个特定结果导致问题,那么它在捕获预期错误(通过 try-catch)方面相当有用,记录它,并通过继续转到下一个结果。 在我看来,“继续”对于在非正常时间执行作业的无人值守服务特别有用,并且一个异常不应影响其他 x 条记录。
If you are iterating through any kind of a result set, and performing operations on said results, for e.g within a for each, and if one particular result caused a problem, its rather useful in capturing an expected error (via try-catch), logging it, and moving on to the next result via continue. Continue is especially useful, imo, for unattended services that do jobs at odd hours, and one exception shouldn't affect the other x number of records.
就这位程序员而言,嵌套 if/else 被认为是有害的。
As far as this programmer is concerned, Nested if/else considered harmful.
在循环开始时使用 continue 来避免迭代不必要的元素并没有害处,而且非常有用,但是在嵌套的 if 和 else 中间使用它可以将循环代码变成复杂的迷宫,以便于理解和理解validate。
我认为它的使用回避也是语义误解的结果。 那些从未在代码中看到/写过“Continue”关键字的人,当看到带有 continue 的代码时,可以将其解释为“自然流程的延续”。 例如,如果我们使用下一步而不是继续,我认为更多的人会欣赏这个有价值的光标功能。
Using continue at the beginning of a loop to avoid iteration over unnecessary elements is not harmful and can be very useful, but using it in the middle of nested ifs and elses can turn the loop code into a complex maze, to understand and validate.
I think its usage avoidance is also the result of a semantic misunderstanding. People who does never see/write 'continue' keyword on their code, when seeing a code with continue can interpret it as "the continuation of the natural flow". If instead of continue we had next, for instance, I think more people would appreciate this valuable cursor feature.
goto 可以用作继续,但反之则不行。
你可以“转到”任何地方,从而任意打破流量控制。
这样继续下去,就不会那么有害了。
goto can be used as a continue, but not the reverse.
You can "goto" anywhere, thus break flow control arbitrarily.
Thus continue, not nearly as harmful.
其他人已经暗示过......但是 continue 和 break 是由编译器强制执行的,并且有自己的关联规则。 Goto 没有这样的限制,尽管在某些情况下净效果可能几乎相同。
我不认为 continue 或 break 本身是有害的,尽管我确信任何一个使用不当都会让任何理智的程序员感到恶心。
Others have hinted at it... but continue and break are enforced by the compiler and have their own associated rules. Goto has no such limitations, though the net effect might almost be the same, in some circumstances.
I do not consider continue or break to be harmful per se, though I'm sure either can be used poorly in a way that would make any sane programmer gag.
我喜欢在循环开始时使用 continue 来处理简单的 if 条件。
对我来说,它使代码更具可读性,因为没有额外的嵌套,并且您可以看到我已经明确处理了这些情况。
这与我使用 goto 的原因相同吗? 也许。 我有时确实使用它们来提高可读性并停止代码嵌套,但我通常更多地使用它们来进行清理/错误处理。
I like to use continue at the beginning of loops for handling simple if conditions.
To me it makes the code more readable since there is not extra nesting and you can see that I have explicitly dealt with these cases.
Is this the same reason that I would use a goto? Perhaps. I do use them for readability at times and to stop the nesting of code but I usually use them more for cleanup/error handling.
没有有害的关键字。 它们只有有害的用途。
Goto 本身并无害处,Continue 也无害。 它们需要小心使用,仅此而已。
There are not harmful keywords. There's only harmful uses of them.
Goto is not harmful per se, neither is continue. They need to be used carefully, that's all.
如果继续导致可读性问题,那么很可能您还有其他问题。 例如,for 循环内有大量代码。 如果您必须编写大型 for 循环,我会尝试坚持使用靠近 for 循环顶部的 continue 。 否则,深埋在 for 循环中间的 continue 很容易被错过。
If continue is causing a problem with readability, then chances are you have other problems. For example, massive amounts of code inside a for loop. If you have to write large for loops, I would try to stick to using continue close to the top of the for loop. Otherwise, a continue buried deep in the middle of a for loop can easily be missed.
无论是否继续,您都可以编写出好的代码,无论是否继续,您都可以编写糟糕的代码。
关于 goto 的参数可能有一些重叠,但就我而言, continue 的使用相当于在方法体中的任何位置使用break语句(在循环中)或return语句 - 如果使用正确,它可以简化代码(不太可能包含错误,更容易维护)。
You can write good code with or without continue and you can write bad code with or without continue.
There probably is some overlap with arguments about goto, but as far as I'm concerned the use of continue is equivalent to using break statements (in loops) or return statement from anywhere in a method body - if used correctly it can simplify the code (less likely to contain bugs, easier to maintain).
我认为反对继续的底线论点是它使得证明代码正确变得更加困难。 这是数学意义上的证明。 但这对你来说可能并不重要,因为没有人有资源来“证明”一个非常复杂的计算机程序。
输入静态分析工具。 你可能会让事情变得更难......
而 goto,出于同样的原因,这听起来像是一场噩梦,但在代码中的任何随机位置。
I believe the bottom line argument against continue is that it makes it harder to PROVE that the code is correct. This is prove in the mathematical sense. But it probably doesn't matter to you because no one has the resources to 'prove' a computer program that is significantly complex.
Enter the static-analysis tools. You may make things harder on them...
And the goto, that sounds like a nightmare for the same reasons but at any random place in code.
我会说是的。 对我来说,它只是破坏了一段流畅编写的代码的“流程”。
另一个论点还可能是,如果您坚持使用大多数现代语言支持的基本关键字,那么您的程序流(如果不是逻辑或代码)可以移植到任何其他语言。 使用不受支持的关键字(即 continue 或 goto)会破坏这一点。
这实际上更多的是个人喜好,但我从来没有使用过它,并且在编写新代码时并不真正认为它是一个选项。 (与转到相同。)
I'd say yes. To me, it just breaks the 'flow' of a fluidly-written piece of code.
Another argument could also be that if you stick to the basic keywords supported by most modern languages, then your program flow (if not the logic or code) could be ported to any other language. Having an unsupported keyword (ie, continue or goto) would break that.
It's really more of a personal preference, but I've never had to use it and don't really consider it an option when I'm writing new code. (same as goto.)
继续
是否比中断
更有害?如果有的话,在我遇到/使用它的大多数情况下,我发现它使代码更清晰并且不像意大利面条一样。
Is
continue
any more harmful than, say,break
?If anything, in the majority of cases where I encounter/use it, I find it makes code clearer and less spaghetti-like.
我认为应该更多地使用 continue!
我经常遇到这样的代码:
而不是
使用它来使代码更具可读性!
I think there should be more use of continue!
Too often I come across code like:
instead of
Use it to make the code more readable!