注:机翻,未校。
Go To Statement Considered Harmful
goto 语句有害论
Edsger W. Dijkstra
埃德斯加·W·迪杰斯特拉
Reprinted from Communications of the ACM, Vol. 11, No. 3, March 1968, pp. 147 - 148. Copyright © 1968, Association for Computing Machinery, Inc.
转载自《美国计算机协会通讯》1968年3月第11卷第3期,第147 - 148页。版权所有©1968,美国计算机协会。
This is a digitized copy derived from an ACM copyrighted work. It is not guaranteed to be an accurate copy of the author’s original work.
这是一份源自美国计算机协会版权作品的数字化副本,不能保证其与作者原始作品完全一致。
Key Words and Phrases:
关键词和短语:
go to 语句、跳转指令、分支指令、条件子句、选择子句、重复子句、程序可读性、程序顺序控制
CR Categories:
CR 分类:
4.22、6.23、5.24
Editor:
编者按:
For a number of years I have been familiar with the observation that the quality of programmers is a decreasing function of the density of go to statements in the programs they produce. More recently I discovered why the use of the go to statement has such disastrous effects, and I became convinced that the go to statement should be abolished from all “higher level” programming languages (i.e. everything except, perhaps, plain machine code). At that time I did not attach too much importance to this discovery; I now submit my considerations for publication because in very recent discussions in which the subject turned up, I have been urged to do so.
多年来,我一直留意到这样一种现象:程序员所编写程序中 goto 语句的密集程度越高,其编程水平就越低。最近我弄清楚了 goto 语句的使用为何会产生如此糟糕的影响,并且坚信 goto 语句应该从所有“高级”编程语言(或许,这里指的是除了纯机器码之外的所有语言)中剔除。当时我并没有太重视这一发现;现在我将这些思考提交发表,是因为在最近相关主题的讨论中,有人极力劝我这么做。
My first remark is that, although the programmer’s activity ends when he has constructed a correct program, the process taking place under control of his program is the true subject matter of his activity, for it is this process that has to accomplish the desired effect; it is this process that in its dynamic behavior has to satisfy the desired specifications. Yet, once the program has been made, the “making” of the corresponding process is delegated to the machine.
我的第一个观点是,尽管程序员在构建出正确的程序后,其编程活动就结束了,但程序所控制的进程才是其编程活动真正的核心内容。因为正是这个进程需要实现预期效果,正是这个进程在动态运行中需要满足预期的规范。然而,一旦程序编写完成,相应进程的“运行”就交给了机器。
My second remark is that our intellectual powers are rather geared to master static relations and that our powers to visualize processes evolving in time are relatively poorly developed. For that reason we should do our utmost to shorten the conceptual gap between the static program and the dynamic process, to make the correspondence between the program (spread out in text space) and the process (spread out in time) as trivial as possible.
我的第二个观点是,我们的智力更倾向于掌握静态关系,而我们对随时间演变的进程进行可视化想象的能力则相对较弱。因此,我们应该竭尽全力缩小静态程序与动态进程之间的概念差距,使程序(在文本空间中展开)和进程(在时间中展开)之间的对应关系尽可能简单明了。
Let us now consider how we can characterize the progress of a process. If the program text is a pure concatenation of assignment statements it is sufficient to point in the program text to a point between two successive action descriptions. (In the absence of go to statements I can permit myself the syntactic ambiguity in the last three words of the previous sentence: if we parse them as “successive (action descriptions)” we mean successive in text space; if we parse as “(successive action) descriptions” we mean successive in time.) Let us call such a pointer to a suitable place in the text a “textual index.”
现在让我们思考如何描述一个进程的进展。如果程序文本仅仅是赋值语句的简单拼接,那么在程序文本中指出两个连续动作描述之间的某个点就足以描述进程的进展。(在没有 goto 语句的情况下,我可以允许上一句话最后三个词存在句法歧义:如果我们将其解析为“连续的(动作描述)” ,指的是在文本空间中的连续;如果解析为“(连续动作)的描述” ,指的是在时间上的连续。)我们把指向文本中合适位置的这种指针称为“文本索引”。
When we include conditional clauses (if B then A ), alternative clauses (if B then A 1 A1 A1 else ( A 2 ) (A2) (