使用 goto 有什么问题?

发布于 2024-09-15 08:47:22 字数 491 浏览 3 评论 0原文

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

我通过 xkcd 进行随机操作,看到了这个(如果还读到了一些负面的内容)几年前关于他们的文字):
你的网速很慢,请使用更快的网速来查看此图片
它到底有什么问题呢?那么为什么 goto 在 C++ 中也是可能的呢?

为什么我应该使用它们?

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):
your slow connection sucks, get a faster one to see this image
What is actually wrong with it? Why are goto's even possible in C++ then?

Why should I not use them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦巷 2024-09-22 08:47:22

因为它们会导致意大利面条代码

过去,编程语言没有while循环、if语句等,程序员使用goto来组成程序的逻辑。它会导致无法维护的混乱。

这就是为什么 CS 之神创造了方法、条件和循环。 结构化编程在当时是一场革命。

goto 在某些地方适用,例如跳出嵌套循环。

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.

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

gotos are appropriate in a few places, such as for jumping out of nested loops.

千と千尋 2024-09-22 08:47:22

如果使用得当,goto 没有任何问题。它之所以成为“禁忌”,是因为在 C 的早期,程序员(通常来自汇编背景)会使用 goto 来创建极其难以理解的代码。

大多数时候,您无需 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:

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

使用goto来实现子程序是其被滥用的主要方式。这会产生所谓的“意大利面条代码”,这些代码不必要地难以阅读和维护。

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.

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:

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.

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.

吖咩 2024-09-22 08:47:22

goto 本身并没有什么问题。它是编程中非常有用的构造,并且有许多有效的用途。我想到的最好的办法是 C 程序中的结构化资源释放。

goto 出错的地方是它被滥用了。滥用 goto 可能会导致代码完全不可读且无法维护。

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.

Where goto goes wrong is when it is abused. Abuse of goto can lead to thoroughly unreadable and unmaintainable code.

未央 2024-09-22 08:47:22

1968 年,Edsger Dijkstra 给《ACM 通讯》的编辑写了一封著名的信 /em> GOTO 被认为是有害的,他在其中列出了使用 while 循环if...then...else 条件进行结构化编程的情况。当使用 GOTO 代替这些控制结构时,结果通常是意大利面条代码。当今使用的几乎每种编程语言都是结构化编程语言,并且 GOTO 的使用几乎已被淘汰。事实上,Java、Scala、Ruby 和 Python 根本没有 goto 命令。

C、C++ 和 Perl 仍然有 GOTO 命令,并且在某些情况下(特别是在 C 中) GOTO 很有用,例如,退出多个循环的break语句,或者作为将清理代码集中在函数中单个位置的方法,即使有多种方法终止函数(例如,通过在函数进程中的多个点)。但一般来说,它的使用应该仅限于以受控和认可的方式调用它的特定设计模式。

(在 C++ 中,最好使用 RAII 或 ScopeGuard (more) 而不是使用 GOTO 进行清理。但是 GOTO 是一个 Linux 内核中常用的习惯用法 (另一个来源)这是惯用的 C 代码的一个很好的例子。)

XKCD 漫画是一个笑话关于“当某些特定的设计模式通过使用 GOTO 得到很大帮助时,是否应该始终认为 GOTO 是有害的?”

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.

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.

(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.)

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?"

2024-09-22 08:47:22

你用谷歌搜索过这个问题吗?

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

要开始使用,您可以转到(哈哈!)http://en.wikipedia.org/wiki/GOTO

Did you google the issue?

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

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

国粹 2024-09-22 08:47:22

它在 C++ 中是可能的,因为它在 C 中是可能的。是否应该使用它是一个长期存在的宗教战争。

C++ 的设计目标是,99% 的 C 程序在编译和运行时应具有与 C++ 相同的功能。包括 goto 是实现该目标的努力的一部分。

It's possible in C++ because it's possible in C. Whether you should or shouldn't use it is long-standing religious war.

It was a design goal of C++ that 99-point-something percent of C programs in the wild should compile and run with the same functionality in C++. Including goto was part of the effort to reach that goal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文