什么是goto的概率 [英] what's the prob with goto

查看:70
本文介绍了什么是goto的概率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C世界很陌生......

$ b $我的导师告诉他们不要使用goto stmt ..

但没有人可以给我为什么会这样满意答案..


plz帮助我摆脱这种困境,因为我在我的代码中使用了很多goto ....

解决方案

尽管goto没有被直接使用,但它们隐含地是编程语言的控制结构的一部分。


gotos会降低程序的可重复性。如果编程

跨越页面,那么很难理解带有gotos的

程序。

Vishal Naidu说:

我对C世界很新......

我的导师告诉我不要使用goto stmt。 。


一个聪明的学生服从他的导师,除非他知道他们错了。

但没有人能给我一个满意的答案,为什么它如此..


并非所有教师都理解他们给出的建议。那不是
必然意味着它是不好的建议。

plz帮助我摆脱这种困境,因为我在我的代码中使用了很多goto ....




问题是goto不能很好地扩展。你使用它越多,你的程序将要维护的难度就越大,因为你的b / b
程序的控制流程变得更难以阅读。这种现象被称为spaghetti

code,并且有充分的理由。


在许多组织中,goto实际上完全被禁止。因此,学习如何在没有它的情况下进行编程是一个很好的想法。一旦你这样做了,你就会想知道你在里面看到了什么。

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 1999年7月29日
http://www.cpax.org.uk

电子邮件rjh在上述域名




Vishal Naidu写道:

我对C世界很陌生......

我的导师告诉我不要使用goto stmt ..
但没有人能给我一个满意的答案为什么会这样..

PLZ帮助我摆脱这种困境,因为我在我的代码中使用了很多goto ....




糟糕的结构化代码比非结构化的代码更容易调试和维护




你不需要非常使用goto经常。使用goto

的一个问题是它可能会破坏通过检查调试代码的能力。这不仅仅是goto本身的问题,而是围绕goto目标的代码中引入的

不确定性。对于

实例,给定代码片段:


i = 7;

label:printf("%d \ n" ;,i);


为任何特定的执行路径打印什么值?直到我为每个goto label;的实例说明
,我都不知道。我必须跟踪所有可能的执行路径才能跟踪所有可能的执行路径,然后才能说出在什么条件下打印的任何

信心。这使代码

更难调试和维护。上帝保佑我不得不做任何改变,因为我不能确信任何改变都不会无意中影响*某些*执行路径。


我想讲的故事是我在电气工程师写的职业生涯早期偶然发现的一个程序。一个5000行的main()函数,

有15个gotos,其中一些向前分支,有些向后。

我的同事花了整整两个星期来计算该该死的东西实际上是如何运作的。我们的任务是改善应用程序的性能,

但我们做的任何事情都会破坏代码。我们终于建议客户只需购买更快的硬件;它会比使用更快的代码更便宜。


现在,这个特定的应用程序超出了使用范围的病态

gotos,但他们的情况很糟糕。如果代码有点结构化,我们可能会做出一些改进。


Gotos在有限的情况下很有用,比如当你需要时br />
打破深度嵌套循环,或者需要确保一些清理

代码在退出例程之前运行。但是他们不应该替换

控制结构,比如if-else,for,while,do-while,或者切换

语句。


如果你打算使用goto,请确保你向前转*只* *和

你没有分支到一个区块。


i m new to the C world...

i ve been told by my instructors not to use goto stmt..
but no one could give me a satisfactory answer as to why it is so..

plz help me out of this dilemma, coz i use a lot of goto in my codes....

解决方案

Even though goto were not used directly, but they are implicitly part
of control constructs of a programming lang.

gotos would reduce the redability of the program. If the programming
spanning across pages, it owuld be really difficult to understand the
programs with gotos.


Vishal Naidu said:

i m new to the C world...

i ve been told by my instructors not to use goto stmt..
A wise student obeys his instructors unless he knows them to be wrong.
but no one could give me a satisfactory answer as to why it is so..
Not all instructors understand the advice they give. That doesn''t
necessarily mean it is bad advice.
plz help me out of this dilemma, coz i use a lot of goto in my codes....



The problem is that goto doesn''t scale well. The more you use it, the harder
your programs will be to maintain, because the flow of control of your
program becomes harder to read. This phenomenon is known as "spaghetti
code", and with good reason.

In many organisations, goto is actually banned altogether. So it''s a good
idea to learn how to program without it. Once you have done so, you''ll
wonder what you ever saw in it.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29 July 1999
http://www.cpax.org.uk
Email rjh at the above domain



Vishal Naidu wrote:

i m new to the C world...

i ve been told by my instructors not to use goto stmt..
but no one could give me a satisfactory answer as to why it is so..

plz help me out of this dilemma, coz i use a lot of goto in my codes....



Bad structured code is orders of magnitude easier to debug and maintain
than bad unstructured code.

You shouldn''t need to use goto very often. One problem with using goto
is that it can destroy the ability to debug code by inspection. It''s
not so much the goto itself that''s the problem, but rather than the
uncertainty introduced in the code around the target of the goto. For
instance, given the code fragment:

i = 7;
label: printf("%d\n", i);

What value gets printed for any particular path of execution? Until I
account for every instance of "goto label;", I don''t know. I have to
trace all possible paths of execution before I can say with any
confidence what gets printed under what conditions. This makes code
harder to debug and maintain. God forbid I have to make any changes,
because I can''t say with any confidence that any change will not
inadvertantly affect *some* path of execution.

The story I like to tell is one program I stumbled across early in my
career written by an electrical engineer. A 5000-line main() function,
with something 15 gotos, some of which branched forward, some backward.
It took my co-worker two full weeks to figure out how the damn thing
actually worked. We were tasked with improving performance of the app,
but anything we did simply broke the code. We finally recommended that
the customer just buy faster hardware; it would be cheaper than the
effort to make the code faster would cost.

Now, that particular application was pathological beyond the use of
gotos, but they made a bad situation impossible. Had the code been
somewhat structured, we probably could have made some improvements.

Gotos are useful in a limited set of circumstances, like when you need
to break out of a deeply nested loop, or need to make sure some cleanup
code runs before exiting a routine. But they should not replace
control structures like if-else , for, while, do-while, or switch
statements.

If you''re going to use a goto, make sure you branch forward *only*, and
that you do not branch into a block.


这篇关于什么是goto的概率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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