每个人都知道Dijkstra的《致编辑的信》:goto语句被认为是有害的(这里。html transcript和这里。pdf),从那时起,就有一种强大的推动力,尽可能避免使用goto语句。虽然可以使用goto来生成不可维护的、庞大的代码,但它仍然存在于现代编程语言中。即使Scheme中先进的连续控制结构也可以被描述为复杂的后向。
在什么情况下需要使用goto?什么时候最好避免?
作为一个后续问题:C提供了一对函数setjmp()和longjmp(),它们不仅提供了在当前堆栈帧内进行跳转的能力,还提供了在任何调用帧内进行跳转的能力。这些应该被认为和goto一样危险吗?更危险?
Dijkstra自己也对这个头衔感到后悔,因为这不是他的责任。在EWD1308的结尾(也在这里。pdf),他写道:
Finally a short story for the record.
In 1968, the Communications of the ACM
published a text of mine under the
title "The goto statement considered
harmful", which in later years would
be most frequently referenced,
regrettably, however, often by authors
who had seen no more of it than its
title, which became a cornerstone of
my fame by becoming a template: we
would see all sorts of articles under
the title "X considered harmful" for
almost any X, including one titled
"Dijkstra considered harmful". But
what had happened? I had submitted a
paper under the title "A case against
the goto statement", which, in order
to speed up its publication, the
editor had changed into a "letter to
the Editor", and in the process he had
given it a new title of his own
invention! The editor was Niklaus
Wirth.
Donald E. Knuth写了一篇关于这个主题的经典论文,它与Dijkstra的论文相匹配,那就是结构化编程与语句。阅读既有助于重建上下文,也有助于对主题的非教条性理解。在本文中,Dijkstra对这个案例的观点被报道,并且更加强烈:
Donald E. Knuth:我相信通过呈现这样一个
事实上,我并不是不同意
Dijkstra的观点,因为
他最近写道:
“请不要落入。
相信我是可怕的
教条的关于[去]的教条的
声明)。我觉得很不舒服
感觉别人在做
宗教出来了,好像
编程的概念问题
只用一个小技巧就能解决,用什么
一种简单的编码纪律!”
被Jay Ballou添加的答案所吸引,我会加入0.02英镑。如果Bruno Ranschaert还没有这样做,我就会提到Knuth的“用GOTO语句进行结构化编程”的文章。
有一件事我没有看到讨论,那就是那种在Fortran教科书中教过的代码,尽管它并不常见。例如DO循环的扩展范围和开放代码子程序(记住,这将是Fortran II, Fortran IV或Fortran 66 -而不是Fortran 77或90)。至少有可能语法细节不准确,但概念应该足够准确。每种情况下的代码片段都在单个函数中。
请注意,由Kernighan和Plauger撰写的优秀但过时(并且绝版)的《编程风格的元素,第二版》中包含了一些来自那个时代(70年代末)编程教科书中滥用GOTO的现实例子。然而,下面的材料并不是来自那本书。
DO循环的扩展范围
do 10 i = 1,30
...blah...
...blah...
if (k.gt.4) goto 37
91 ...blah...
...blah...
10 continue
...blah...
return
37 ...some computation...
goto 91
One reason for such nonsense was the good old-fashioned punch-card. You might notice that the labels (nicely out of sequence because that was canonical style!) are in column 1 (actually, they had to be in columns 1-5) and the code is in columns 7-72 (column 6 was the continuation marker column). Columns 73-80 would be given a sequence number, and there were machines that would sort punch card decks into sequence number order. If you had your program on sequenced cards and needed to add a few cards (lines) into the middle of a loop, you'd have to repunch everything after those extra lines. However, if you replaced one card with the GOTO stuff, you could avoid resequencing all the cards - you just tucked the new cards at the end of the routine with new sequence numbers. Consider it to be the first attempt at 'green computing' - a saving of punch cards (or, more specifically, a saving of retyping labour - and a saving of consequential rekeying errors).
哦,你可能还注意到我在作弊,没有大喊大叫——Fortran IV通常都是大写的。
中非子例程
...blah...
i = 1
goto 76
123 ...blah...
...blah...
i = 2
goto 76
79 ...blah...
...blah...
goto 54
...blah...
12 continue
return
76 ...calculate something...
...blah...
goto (123, 79) i
54 ...more calculation...
goto 12
标签76和54之间的GOTO是计算GOTO的一个版本。如果变量i的值为1,则转到列表中的第一个标签(123);如果它的值是2,就转到秒,以此类推。从76到计算goto的片段是开放编码的子程序。它是一段执行起来很像子例程的代码,但写在函数体中。(Fortran也有语句函数——它们是嵌入在单行上的函数。)
还有比计算goto更糟糕的结构——你可以给变量赋标签,然后使用赋值的goto。google assigned goto告诉我它已经从Fortran 95中删除了。值得注意的是,结构化编程革命可以说是从Dijkstra的“GOTO被认为是有害的”信件或文章开始的。
如果不了解Fortran语言(以及其他语言,其中大多数已经半途而用了)中所做的事情,我们这些新手很难理解Dijkstra所处理的问题的范围。见鬼,直到那封信发表10年后,我才开始编程(但我确实不幸地在Fortran IV中编程了一段时间)。
以下陈述是概括;尽管抗辩例外总是可能的,但通常(以我的经验和拙见)不值得冒险。
Unconstrained use of memory addresses (either GOTO or raw pointers) provides too many opportunities to make easily avoidable mistakes.
The more ways there are to arrive at a particular "location" in the code, the less confident one can be about what the state of the system is at that point. (See below.)
Structured programming IMHO is less about "avoiding GOTOs" and more about making the structure of the code match the structure of the data. For example, a repeating data structure (e.g. array, sequential file, etc.) is naturally processed by a repeated unit of code. Having built-in structures (e.g. while, for, until, for-each, etc.) allows the programmer to avoid the tedium of repeating the same cliched code patterns.
Even if GOTO is low-level implementation detail (not always the case!) it's below the level that the programmer should be thinking. How many programmers balance their personal checkbooks in raw binary? How many programmers worry about which sector on the disk contains a particular record, instead of just providing a key to a database engine (and how many ways could things go wrong if we really wrote all of our programs in terms of physical disk sectors)?
以上附注:
关于第2点,考虑以下代码:
a = b + 1
/* do something with a */
在代码中的“do something”点,我们可以高度自信地声明a大于b。(是的,我忽略了未捕获整数溢出的可能性。我们不要拘泥于一个简单的例子。)
另一方面,如果代码是这样读的:
...
goto 10
...
a = b + 1
10: /* do something with a */
...
goto 10
...
标记10的方法的多样性意味着我们必须更加努力才能确信a和b在这一点上的关系。(事实上,在一般情况下,这是不可判断的!)
关于第4点,代码中“去某个地方”的整个概念只是一个比喻。除了电子和光子(用于余热),CPU内部没有任何东西真正“去”到任何地方。有时候,我们会放弃一个比喻,转而使用另一个更有用的比喻。我记得(几十年前!)遇到过一种语言
if (some condition) {
action-1
} else {
action-2
}
通过将action-1和action-2编译为行外无参数例程,然后使用单个双参数VM操作码(使用条件的布尔值来调用其中一个)在虚拟机上实现。这个概念只是“选择现在调用什么”,而不是“去这里或去那里”。再一次,换一个比喻。