goto 的可接受用途是什么?
可能的重复:
C 或 C++ 中良好的 goto 示例
goto 语句您认为可以接受吗?我正在讨论一些 C 嵌入式工作的编码指南,并想知道是否在某些情况下 goto 是最简洁的做事方式。
Possible Duplicate:
Examples of good gotos in C or C++
What uses of the goto statement do you consider acceptable? I am discussing some coding guidelines for C embedded work, and wondering if there are cases where goto is the cleanest way to do things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 C,我发现在某些函数中使用 goto 作为公共退出点很有用。如果您需要在返回之前释放资源,最好执行 retval = -ERROR; goto fn_exit; 而不是尝试跳出多层 for/while 循环。
有些人会认为,在设计良好的代码中没有必要这样做——当您达到使用 goto 有吸引力的程度时,您应该将该函数分解为多个子函数。也许在某些情况下确实如此,但如果您必须传递多个变量,或者传递指向变量的指针,以便子函数可以更新这些值,我觉得您增加了不必要的复杂性。
这是最近关于使用 goto 进行错误管理。我确信浏览 goto 标签 会给您带来更多答案。
For C, I find it useful to use goto for a common exit point in some functions. If you need to release resources before returning, it's nice to do
retval = -ERROR; goto fn_exit;
instead of trying to break out of multiple layers of for/while loops.Some would argue that it isn't necessary in well designed code -- when you reach a point where using goto is attractive, you should be breaking the function up into multiple sub-functions. Maybe that's true in some cases, but if you have to pass multiple variables in, or pass pointers to variables so a sub-function can update those values, I feel like you've added unnecessary complications.
Here's a recent SO question on using goto for error management. I'm sure that browsing the goto tag will get you even more answers.
我发现goto有不少地方可以简化逻辑。我的规则是两个 goto 不能互相重叠,并且 goto 和它的标签是“接近”的;通常,间隔不超过 30 行。
通常,当我遇到复杂的情况,必须检查很多事情时,goto 可能会有所帮助:
I found that there are quite a few places where goto might simplify the logic. My rule is that no 2 goto's overlap each other and that the goto and its label are 'close'; usually, no more than 30 lines apart.
Usually, when I have a complex condition that I have to check many things, a goto might help:
我个人的观点是,在现代编程语言中,goto 语句的用法是不可接受的。
已故的 Edsger W. Dijkstra 所著的《GOTO 声明被认为是有害的》很好地涵盖了这个问题。这篇论文应该是地球上每个软件开发人员的必读书目。
值得注意的是,吉普赛语言是 20 世纪 70 年代末由 UT Austin 的 Don Good 小组开发的,没有 goto。
值得注意的是,Ichbiah 等人只在 Ada 中包含了 goto,因为 DoD 在需求规范中用很多字明确地要求它。我记得读过 Ichbiah 和他的团队故意使 goto 目标标签语法变得尽可能丑陋,以使标签像拇指酸痛一样突出,并阻止使用 goto。
My personal opinion is that there are NO acceptable uses of the goto statement in a modern programming language.
"GOTO Statement Considered Harmful", by the late Edsger W. Dijkstra, does a good job of covering the issue. That paper should be required reading for every software developer on the planet.
It is worth noting that the Gypsy language, from Don Good's group at UT Austin in the late 1970s, did not have a goto.
It is worth noting that Ichbiah et al only included a goto in Ada because the DoD required it, explicitly, in so many words in the requirements specification. I remember reading that Ichbiah and his team deliberately made the goto target label syntax as ugly as they could, to make the labels stick out like sore thumbs, and discourage use of the goto.