关于“goto”在C. [英] regarding "goto" in C

查看:61
本文介绍了关于“goto”在C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,

需要一些常见的观点

我们可以选择使用goto在C语言中,但是大多数测试书

(甚至K& R)建议不要使用它。

我的个人经验是goto有时会使程序更多

更干净,易于理解,也非常有用(错误处理

案例)。

为什么goto被文明c程序员社区禁止。


是否有任何技术效率低下。


谢谢

Guys,
Need some of your opinion on an oft beaten track
We have an option of using "goto" in C language, but most testbooks
(even K&R) advice against use of it.
My personal experience was that goto sometimes makes program some more
cleaner and easy to understand and also quite useful (in error handling
cases).
So why goto is outlawed from civilized c programmers community.

is there any technical inefficiency in that.

Thank you

推荐答案

MB写道:
M.B wrote:
需要一些常见的观点
我们可以选择使用goto用C语言,但大多数测试手册(甚至K& R)建议不要使用它。
我个人的经验是,goto有时会让程序更清洁,更容易理解,而且相当有用(在错误处理案件中)。
为什么goto被文明c程序员社区禁止。


大约1968年左右Edsger Dijkstra写了一篇文章,作为一封信发表给ACM通讯杂志编辑的一封信

,名为Go To

被认为是有害的。他观察到无限制地使用goto - 我们现在称之为意大利面条代码 - 使程序难以理解,因此
因此经常出现错误。不久之后,开发一种编程方法(称为结构化编程)有很多活动,这需要将b * b分解成整齐的块,例如迭代,序列和

选择,这样就不需要了!它是一种以b
组织代码的方式。


它仍然存在的原因是,作为一般规则是它

仍然有助于防止意大利面条代码混乱。它总能产生更易理解的代码吗?我不这么认为,虽然它通常是

关闭。在我看来,在尝试破解它之前,应该理解并使用它是一个规则。

是否有任何技术效率低下。
Need some of your opinion on an oft beaten track
We have an option of using "goto" in C language, but most testbooks
(even K&R) advice against use of it.
My personal experience was that goto sometimes makes program some more
cleaner and easy to understand and also quite useful (in error handling
cases).
So why goto is outlawed from civilized c programmers community.
About 1968 or so Edsger Dijkstra wrote an article, published as a Letter
to the Editor in the ACM Communications magazine, titled Go To
Considered Harmful. He observed that unrestrained use of goto--what we
now refer to as "spaghetti code"--makes a program hard to understand and
therefore often has bugs. Shortly afterwards, there was much activity
to develop a method of programming, called structured programming, that
decomposed code into neat blocks, such as iteration, sequence, and
selection, such that no gotos were needed! It caught on as a way to
organize code.

The reason it is still with us is that as a general rule is that it
still helps to prevent the spaghetti code mess. Does it always produce
more understandable code? I don''t think so, although it usually comes
close. In my opinion, it is a rule that should be understood and used
well before attempting to break it.
is there any technical inefficiency in that.




一般来说,消除goto会有一些效率低下。

为了消除goto,你经常需要实现额外的

变量或标志将信息从内部循环传送到

外部以停止中间处理,如果

你只是直接跳出一个环。此外,往往会有一些代码重复的b
代码。有些人,比如我,通过从身体中多个地方返回来作弊,违反了严格的

结构化方法。


在机器级别,几乎总是有一个执行goto功能的跳转或分支

指令。


-

Thad



In general, there is some inefficiency with the elimination of goto.
In order to eliminate goto, you often have to implement additional
variables or flags to carry information from an inner loop to the
outside in order to stop intermediate processing, which is not needed if
you simply jump directly out of a loop. Also, there tends to be some
code duplication. Some people, such as I, cheat by returning from a
function more than one place in the body, a violation of the strict
structured approach.

At the machine level, there is almost always a jump or branch
instruction that performs a goto function.

--
Thad


" M.B" < MB ******* @ gmail.com>写道:
"M.B" <mb*******@gmail.com> writes:
需要一些常见的观点
我们可以选择使用goto用C语言,但大多数测试手册(甚至K& R)建议不要使用它。
我个人的经验是,goto有时会让程序更清洁,更容易理解,而且相当有用的(错误处理
情况)。
为什么goto被文明c程序员社区禁止。
Need some of your opinion on an oft beaten track
We have an option of using "goto" in C language, but most testbooks
(even K&R) advice against use of it.
My personal experience was that goto sometimes makes program some more
cleaner and easy to understand and also quite useful (in error handling
cases).
So why goto is outlawed from civilized c programmers community.




它不是取缔"除非你正在使用编码标准,否则b $ b禁止它。


Goto语句很容易导致意大利面条代码。真正的问题

不是goto语句本身;这是标签。每当你在代码中看到一个

标签时,就很难分辨出程序是如何获得的。
得到了它。所谓的结构化编程 intstead从一组更高级别的构造构建一个

程序结构,例如

if / then / else,循环,开关等等。


程序的结构通常对应于现实世界中建模的某个实体。 if语句对应于决策。一个

循环对应于反复做某事。一个goto语句

对应于......好吧,从程序中的一个点跳到另一个点
;它很少匹配现实世界中的任何东西(除非

程序正在建模一个有限状态机)。


小心使用它们可能很有用,特别是作为一个

替代语言中缺少的控制结构。

C没有异常处理机制,所以gotos可能很有用

用于错误处理(当某些东西出现时,b / b
出错了)。 C没有多级中断语句,所以getos

对此有用。


向后分支的goto语句,或者通过循环直接完成

的事情,几乎总是一个坏主意。


经典论文关于这个话题,从1968年开始,是Edsger W. Dijkstra的

Go To Statement Considered Harmful,可在

< http:// www。 acm.org/classics/oct95/> ;.

-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



It''s not "outlawed" unless you''re working with a coding standard that
forbids it.

Goto statements can easily lead to "spaghetti code". The real problem
isn''t the goto statement itself; it''s the label. Whenever you see a
label in code, it''s very difficult to tell how the program could have
gotten there. So-called "structured programming" intstead builds a
program structure from a set of higher-level constructs, such as
if/then/else, loops, switches, and so forth.

A program''s structure often corresponds to something in the real-world
entity being modeled. An if statement corresponds to a decision. A
loop corresponds to doing something repeatedly. A goto statement
corresponds to ... well, to jumping from one point in the program to
another; it rarely matches anything in the real world (unless the
program is modeling a finite state machine).

Used with care, however, they can be useful, especially as a
substitute for a control structure that''s missing from the language.
C doesn''t have an exception handling mechanism, so gotos can be useful
for error handling (bailing out of a nested construct when something
goes wrong). C doesn''t have a multi-level break statement, so gotos
can be useful for that.

A goto statement that branches backward, or that does something that
could have been done straightforwardly with a loop, is almost always a
bad idea.

The classic essay on the topic, from 1968, is Edsger W. Dijkstra''s
"Go To Statement Considered Harmful", available at
<http://www.acm.org/classics/oct95/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Keith Thompson写道:
Keith Thompson wrote:
关于这个主题的经典文章,从1968年开始,是Edsger W. Dijkstra'
Go To Statement Considered Harmful,可在
< http://www.acm.org/classics/oct95/>。
The classic essay on the topic, from 1968, is Edsger W. Dijkstra''s
"Go To Statement Considered Harmful", available at
<http://www.acm.org/classics/oct95/>.

获取。

让我们不要忘记

结构化编程与转到语句
http://portal.acm.org/citation.cfm?id=356640


Let''s not forget
"Structured Programming with go to Statements"
http://portal.acm.org/citation.cfm?id=356640


这篇关于关于“goto”在C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆