什么时候适合使用休息时间?
我的问题基本上都在标题里了。我心里没有真正的应用程序,我只是想确保我使用普遍接受的良好编码实践,我记得我的计算机科学教授说过,通常会尽可能避免中断,但他从未告诉我们为什么。< /em>
我会亲自问他,但他今年夏天不在办公室。
My questions is basically all in the title. I have no real application in mind, I'm just trying to make sure I use generally accepted good coding practices, and I remember my CS professor saying that breaks are generally avoided when possible, but he never told us why.
I would ask him myself, but he's out of his office this summer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是一个神学问题。反对使用 break(或 continue)的人说,这会使程序的控制流更难以遵循,从而降低程序的可读性。但是,制作可读代码的更简单方法就是制作更短的函数、使用有意义的变量名称、适当的注释等。
This is a theological issue. People who are opposed to the use of break (or continue for that matter) say that it makes the control flow of the program harder to follow and thus makes the program less readable. But an easier way to make readable code is simply to make shorter functions, use meaningful variable names, comment appropriately, etc.
有时教授是错的。
他可能会反对在中间跳出循环。这类似于在函数中间使用 return 语句......有些人认为这是亵渎。
应该使用最简单并且生成最容易维护的代码。
Sometimes professors are wrong.
He may object to skipping out of a loop right in the middle of it. This is akin to using a return statement in the middle of a function...some consider that blasphemy.
What ever is the simplest, and produces the most easily maintainable code, is what should be used.
一般来说,当满足某个条件时(例如,当你找到你要找的东西时等),你会使用“break”来跳出循环;我不认为它是“邪恶的”,例如臭名昭著的 GOTO 语句,但理性的人会有所不同。
Generally speaking, you will use 'break' to get out of cycles when a certain condition is met (for instance, when you've found what you're looking for, etc.); I don't consider it 'evil' such as the infamous GOTO statement, but reasonable people will differ.
Dijkstra 所说的关于 goto 语句的有害内容 - http://drdobbs.com/blogs/cpp/228700940
和<一个href="https://softwareengineering.stackexchange.com/questions/58237/are-break-and-continue-bad-programming-practices">https://softwareengineering.stackexchange.com/questions/58237/are-break-并继续糟糕的编程实践
What Dijkstra said was harmful about goto statements - http://drdobbs.com/blogs/cpp/228700940
and https://softwareengineering.stackexchange.com/questions/58237/are-break-and-continue-bad-programming-practices
你的教授说“应该避免中断”,因为它们类似于现在禁忌的 goto 语句。然而,说“应尽可能避免休息”是错误的。
Break 的存在是为了提高可读性。您可以在不中断的情况下做任何事情,但这并不意味着您应该这样做。有时,像“在这种情况下,跳出这个循环”这样的陈述是有意义的。或“在这种情况下,从此函数返回。”
在嵌套 for 循环中对程序流使用 break 是一个坏主意,这可能会给普通读者带来一些困惑。
简单的经验法则:在这里使用break(而不是额外的if/then、另一个布尔值等)会使这更容易理解还是更难理解?就是这么简单。
Your professor was saying "breaks should be avoided" because they are similar to the now-taboo goto statement. However to say "breaks should be avoided whenever possible" is just wrong.
Break exists for readability. You can do anything without break that you can with it, but that doesn't mean you should. Sometimes it just makes sense to make a statement like "In this condition, break out of this loop." or "In this condition, return from this function."
It's a bad idea to use break for program flow in a nested for loops in a way that might give a casual reader some confusion.
Simple rule of thumb: Does using break here (instead of additional if/thens, another boolean, etc.) make this easier to understand or harder to understand? It's that simple.
请您的教授尝试用任何 C 风格语言不间断地编写
switch
语句。我确信这是可以完成的,但是生成的代码将不再具有可读性。Ask your professor to try writing a
switch
statement in any C-style language without break. It can be done I'm sure, but the resulting code is not going to be any more readable.