使用后藤有什么问题?(复制)

时间:2020-12-31 18:33:23

Possible Duplicates:
Why is it bad to use goto?
GOTO still considered harmful?

可能的重复:为什么使用goto不好?GOTO仍然被认为是有害的吗?

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上闲逛,看到了这个(如果几年前也读到一些关于他们的负面文章的话):它到底有什么问题?那么,在c++中为什么goto是可能的呢?

Why should I not use them?

为什么我不应该使用它们?

6 个解决方案

#1


67  

Because they lead to spaghetti code.

因为它们导致了意大利面条式的代码。

In the past programming languages didn't have while() if() etc, and programmers used goto to make up the logic of their programs. It lead to an unmaintainable mess.

在过去的编程语言中,如果()等等,程序员使用goto来弥补程序的逻辑。它会导致不可维护的混乱。

That's why the CS gods created functions conditionals and loops. Structured programming was a revolution at the time.

这就是为什么CS诸神创建了条件和循环函数。结构化编程是当时的一场革命。

goto's are appropriate in a few places, such as for jumping out of nested loops

goto在一些地方是合适的,比如从嵌套循环中跳出

#2


37  

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是有用的。主要的例子是:

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.

使用goto跳出一个深度嵌套的循环,通常比使用条件变量并在每个级别检查它更干净。

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实现子例程是滥用子例程的主要方式。这就产生了所谓的“意大利面条式代码”,读起来和维护起来是不必要的困难。

#3


29  

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.

后藤本身没有什么错。它是编程中非常有用的构造,有许多有效的用途。我想到的最好的方法是在C程序中释放结构化资源。

Where goto's go wrong is when they are abused. Abuse of gotos can lead to thoroughly unreadable and unmaintainable code.

当他们受到虐待时,goto就会出问题。滥用goto会导致完全不可读和不可维护的代码。

#4


19  

In 1968, Edsger Dijkstra wrote a famous letter to the editor of Communications of the ACM GOTO is considered harmful in which he laid out the case for structured programming with while loops and if...then...else conditionals. 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的通讯编辑写了一封著名的信,信中认为他是有害的,他提出了结构化编程的例子,如果……其他条件。当使用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仍有转到命令,有情况(尤其是在C)GOTO是有用的,例如休息声明退出多个循环,或作为一种集中清理代码在一个地方在一个函数中即使有多种方法来终止函数(例如通过返回错误代码在多个点函数的进步)。但是一般来说,它的使用应该被限制在特定的设计模式中,这些模式以一种控制和公认的方式调用它。

(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 (more),而不是使用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漫画是一个关于“当某些特定的设计模式被它的使用极大地帮助时,它是否应该一直被认为是有害的?”

#5


7  

Did you google the issue?

你把问题提出来了吗?

The founder of the anti-goto movement is Edsger Dijskstra with his legendary "Goto Considered Harmful"

反Goto运动的创始人是Edsger Dijskstra,他的传奇作品“Goto被认为是有害的”

To get you started you can goto (ha ha!) http://en.wikipedia.org/wiki/GOTO

要开始你可以访问http://en.wikipedia.org/wiki/GOTO

#6


4  

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中是有可能的。

#1


67  

Because they lead to spaghetti code.

因为它们导致了意大利面条式的代码。

In the past programming languages didn't have while() if() etc, and programmers used goto to make up the logic of their programs. It lead to an unmaintainable mess.

在过去的编程语言中,如果()等等,程序员使用goto来弥补程序的逻辑。它会导致不可维护的混乱。

That's why the CS gods created functions conditionals and loops. Structured programming was a revolution at the time.

这就是为什么CS诸神创建了条件和循环函数。结构化编程是当时的一场革命。

goto's are appropriate in a few places, such as for jumping out of nested loops

goto在一些地方是合适的,比如从嵌套循环中跳出

#2


37  

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是有用的。主要的例子是:

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.

使用goto跳出一个深度嵌套的循环,通常比使用条件变量并在每个级别检查它更干净。

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实现子例程是滥用子例程的主要方式。这就产生了所谓的“意大利面条式代码”,读起来和维护起来是不必要的困难。

#3


29  

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.

后藤本身没有什么错。它是编程中非常有用的构造,有许多有效的用途。我想到的最好的方法是在C程序中释放结构化资源。

Where goto's go wrong is when they are abused. Abuse of gotos can lead to thoroughly unreadable and unmaintainable code.

当他们受到虐待时,goto就会出问题。滥用goto会导致完全不可读和不可维护的代码。

#4


19  

In 1968, Edsger Dijkstra wrote a famous letter to the editor of Communications of the ACM GOTO is considered harmful in which he laid out the case for structured programming with while loops and if...then...else conditionals. 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的通讯编辑写了一封著名的信,信中认为他是有害的,他提出了结构化编程的例子,如果……其他条件。当使用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仍有转到命令,有情况(尤其是在C)GOTO是有用的,例如休息声明退出多个循环,或作为一种集中清理代码在一个地方在一个函数中即使有多种方法来终止函数(例如通过返回错误代码在多个点函数的进步)。但是一般来说,它的使用应该被限制在特定的设计模式中,这些模式以一种控制和公认的方式调用它。

(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 (more),而不是使用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漫画是一个关于“当某些特定的设计模式被它的使用极大地帮助时,它是否应该一直被认为是有害的?”

#5


7  

Did you google the issue?

你把问题提出来了吗?

The founder of the anti-goto movement is Edsger Dijskstra with his legendary "Goto Considered Harmful"

反Goto运动的创始人是Edsger Dijskstra,他的传奇作品“Goto被认为是有害的”

To get you started you can goto (ha ha!) http://en.wikipedia.org/wiki/GOTO

要开始你可以访问http://en.wikipedia.org/wiki/GOTO

#6


4  

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中是有可能的。