C++ 使用 goto 有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3517726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is wrong with using goto?
提问by
Possible Duplicates:
Why is it bad to use goto?
GOTO still considered harmful?
I was ramdomming through xkcd and saw this one (if also read some negative texts about them some years ago):
What is actually wrong with it? Why are goto's even possible in C++ then?
我正在浏览 xkcd 并看到这个(如果几年前还阅读了一些关于它们的负面文本):
它实际上有什么问题?为什么 goto 在 C++ 中甚至是可能的呢?
Why should I notuse them?
为什么我不应该使用它们?
采纳答案by Byron Whitlock
Because they lead to spaghetti code.
因为它们导致了意大利面条式的代码。
In the past, programming languages didn't have while loops, if statements, etc., and programmers used goto to make up the logic of their programs. It lead to an unmaintainable mess.
过去,编程语言没有while循环、if语句等,程序员用goto来组成他们程序的逻辑。它导致无法维护的混乱。
That's why the CS gods created methods, conditionals and loops. Structuredprogramming was a revolution at the time.
这就是 CS 神创造方法、条件和循环的原因。结构化编程在当时是一场革命。
goto's are appropriate in a few places, such as for jumping out of nested loops.
goto 在一些地方是合适的,例如跳出嵌套循环。
回答by bta
Nothing is wrong with goto
if it is used properly. The reason it is "taboo" is because in the early days of C, programmers (often coming from an assembly background) would use goto
to create incredibly hard-to-understand code.
goto
如果使用得当,没有什么问题。它是“禁忌”的原因是因为在 C 的早期,程序员(通常来自汇编背景)会goto
用来创建令人难以置信的难以理解的代码。
Most of the time, you can live without goto
and be fine. There are a few instances, however, where goto
can be useful. The prime example is a case like:
大多数时候,你可以没有goto
并且过得很好。但是,在某些情况下goto
可能会有用。最好的例子是这样的案例:
for (i = 0; i < 1000; i++) {
for (j = 0; j < 1000; j++) {
for (k = 0; k < 1000; k++) {
...
if (condition)
goto break_out;
....
}
}
}
break_out:
Using a goto
to jump out of a deeply-nested loop can often be cleaner than using a condition variable and checking it on every level.
使用 agoto
跳出深度嵌套的循环通常比使用条件变量并在每个级别检查它更干净。
Using goto
to implement subroutines is the main way it is abused. This creates so-called "spaghetti code" that is unnecessarily difficult to read and maintain.
使用goto
来实现子程序是被滥用的主要途径。这会创建所谓的“意大利面条式代码”,它不必要地难以阅读和维护。
回答by JaredPar
There is nothing wrong with goto in itself. It's a very useful construct in programming and has many valid uses. The best that comes to mind is structured resource freeing in C programs.
goto 本身并没有什么问题。它在编程中是一个非常有用的结构,并且有许多有效的用途。想到的最好的方法是 C 程序中的结构化资源释放。
Where goto's go wrong is when they are abused. Abuse of gotos can lead to thoroughly unreadable and unmaintainable code.
goto 出错的地方是它们被滥用时。滥用 goto 会导致完全不可读和不可维护的代码。
回答by Ken Bloom
In 1968, Edsger Dijkstrawrote a famous letter to the editor of Communications of the ACMGOTO is considered harmfulin which he laid out the case for structured programming with while loopsand if...then...elseconditionals. When GOTO is used to substitute for these control structures, the result is very often spaghetti code. Pretty much every programming language in use to day is a structured programming language, and use of GOTOs has been pretty much eliminated. In fact, Java, Scala, Ruby, and Python don't have a goto
command at all.
1968 年,Edsger Dijkstra给ACM GOTO的Communications编辑写了一封著名的信,他在信中阐述了结构化编程与while 循环和if...then...else条件的案例。当使用 GOTO 替代这些控制结构时,结果通常是意大利面条式代码。当今使用的几乎所有编程语言都是结构化的编程语言,并且几乎不使用 GOTO。事实上,Java、Scala、Ruby 和 Python 根本没有goto
命令。
C, C++ and Perl still do have a GOTO command, and there are situations (in C particularly) where a GOTO is useful, for example a break statement that exits multiple loops, or as a way of concentrating cleanup code in a single place in a function even when there are multiple ways to terminate the function (e.g. by returning error codes at multiple points in the progress of a function). But generally its use should be restricted to specific design patterns that call for it in a controlled and recognized way.
C、C++ 和 Perl 仍然有一个 GOTO 命令,并且在某些情况下(特别是在 C 中)GOTO 很有用,例如退出多个循环的 break 语句,或者作为一种将清理代码集中在一个地方的方式一个函数,即使有多种方法可以终止该函数(例如,通过在函数执行过程中的多个点返回错误代码)。但通常它的使用应该仅限于以受控和公认的方式调用它的特定设计模式。
(In C++, it's better to use RAII or a ScopeGuard(more)instead of using GOTO for cleanup. But GOTO is a frequently used idiom in the Linux kernel(another source) which is a great example of idiomatic C code.)
(在 C++ 中,最好使用 RAII 或ScopeGuard (更多)而不是使用 GOTO 进行清理。但 GOTO 是Linux 内核(另一个来源)中经常使用的惯用语,它是惯用 C 代码的一个很好的例子。)
The XKCD comic is a joke on the question "Should GOTO always be considered harmful when there are certain specific design patterns that are helped greatly by its use?"
XKCD漫画是关于“当某些特定的设计模式对其使用有很大帮助时,是否应该始终认为GOTO有害?”这个问题的笑话。
回答by John Smith
Did you google the issue?
你谷歌这个问题吗?
The founder of the anti-goto movement is Edsger Dijskstra with his legendary "Goto Considered Harmful"
反转到运动的创始人是 Edsger Dijskstra 及其传奇的“转到被认为有害”
To get you started you can goto (ha ha!) http://en.wikipedia.org/wiki/GOTO
为了让您开始,您可以转到(哈哈!)http://en.wikipedia.org/wiki/GOTO
回答by Nikolai Fetissov
It's possible in C++ because it's possible in C. Whether you should or shouldn't use it is long-standing religious war.
它在 C++ 中是可能的,因为它在 C 中是可能的。你是否应该使用它是长期的宗教War。