真的很邪恶吗? [英] Are gotos truly evil?

查看:65
本文介绍了真的很邪恶吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有在我的代码中使用过goto语句,我最初的事情之一是

在我的软件课程中告诉我几年前是goto语句是

邪恶并导致spagetti代码 - 不要使用它们。


我吸收了这个口头禅并告诉其他人我完全相信
$ b $它。然而,我最近和某人谈话,他说他多么喜欢goto语句以及他们有多么有用,微软不得不把它们留在c#中,因为他们是如此有用,否则为什么

语句会使用该语言。他并不主张过量使用它们,但是b $ b使用它们来允许自己创建一个模式,允许他在最后的函数中有一个

退出点。我一直试图让他相信

gotos并不好,但他似乎不相信我的论点。


我知道一个常见的用途是人们说要出去深度嵌套的

语句,但你可以通过使用布尔语或用函数调用替换

部分代码轻松完成。


为什么如果它们如此糟糕,仍然使用该语言,该关键字的可接受用途是什么?必须有一些否则为什么

语句在c#语言,或者这只是一个传统的东西来吸引

其他语言的程序员习惯了什么?


我想不出任何你真的有理由使用它们,但也许是其他人的b $ b。

I have never used a goto statement in my code, one of the first things I was
told in my software classes a number of years ago was "goto statements are
evil and lead to spagetti code - do not use them".

I absorbed this mantra and tell this to other people and I fully believe in
it. However, I was talking to someone recently and he was saying how much he
loved goto statements and how useful they were and that Microsoft had to
leave them in c# because they were so useful, otherwise why would the
statement be in the language. He does not advocate using them in excess but
uses them to allow himself to create a pattern that allows him to have one
point of exit in a function at the end. I kept trying to convince him that
gotos were not good but he seemed unconvinced by my arguements.

I know one common use people say is for getting out of deeply nested
statements but you can easily do without gotos by using booleans or replacing
sections of code with function calls.

Why if they are so bad is goto still in the language, what are the
acceptable uses of the keyword, there must be some otherwise why is the
statement in the c# language, or is this just a legacy thing to attract
programmers from other languages who are used to gotos?

I can''t think of any really valid reason why you would use them but maybe
other people have.

推荐答案

在消息< F9 ** ********************************@microsoft.co m> ;, Mark R.

Dawson< Ma ********* @ discussion.microsoft.com>写道
In message <F9**********************************@microsoft.co m>, Mark R.
Dawson <Ma*********@discussions.microsoft.com> writes
我从来没有在我的代码中使用过goto语句,我在几年前的软件类中告诉过的第一件事就是goto语句是
邪恶并导致spagetti代码 - 不要使用它们。

我吸收了这个口头禅并告诉其他人,我完全相信它。然而,我最近和某人谈话,他说他多么喜欢goto语句以及他们有多么有用,而且微软不得不把它们留在c#因为它们非常有用,否则为什么会
声明在语言中。


规则是为了服从傻瓜和智者的指导。

(Douglas Bader)

我不能想到为什么你会使用它们的任何真正有效的理由,但也许是其他人的。
I have never used a goto statement in my code, one of the first things I was
told in my software classes a number of years ago was "goto statements are
evil and lead to spagetti code - do not use them".

I absorbed this mantra and tell this to other people and I fully believe in
it. However, I was talking to someone recently and he was saying how much he
loved goto statements and how useful they were and that Microsoft had to
leave them in c# because they were so useful, otherwise why would the
statement be in the language.
"Rules are for the obedience of fools and the guidance of wise men."
(Douglas Bader)
I can''t think of any really valid reason why you would use them but maybe
other people have.




Nor I.如果我遇到过在使用goto有意义的情况下,我会怀疑地对待它,因为代码的结构可能从根本上是有缺陷的。如果,经过检查,没有更好的写作方式,我会继续使用它。


我认为OO处理具有复杂流程的长方法的思维方式

控制作为代码气味可能有助于编程风格

,其中不会发生问题。


-

Steve Walker



Nor I. If I came across a situation where using goto made sense, I''d
treat it with suspicion, on the grounds that the structure of the code
was probably fundamentally flawed. If, on examination, there wasn''t a
better way of writing it, I''d go ahead and use it.

I think the OO mindset of treating long methods with complex flow
control as a code smell probably contributes to a style of programming
in which the issue does not occur.

--
Steve Walker


" Mark R. Dawson" <马********* @ discussions.microsoft.com>在留言中写道

新闻:F9 ********************************** @ microsof t.com ...
"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
我从来没有在我的代码中使用过goto语句,这是我几年前在我的软件类中告诉我的第一件事。是goto陈述是邪恶的,导致spagetti代码 - 不要使用它们。

我吸收了这个口头禅并告诉其他人,我完全相信

它。然而,我最近和某人谈话,他说他多么喜欢goto语句以及它们有多么有用,而且微软必须将它们留在c#中,因为它们非常有用,否则为什么
声明会使用该语言。他并不主张过度使用它们,而是使用它们来让自己创造一种模式,让他在最后的功能中有一个退出点。我一直试图说服他
得到的并不好,但他似乎不相信我的论点。

我知道一个人们常说的用于摆脱深层嵌套的常用用法br />语句,但你可以轻松地使用布尔语或
用函数调用替换代码部分。

为什么如果它们如此糟糕仍然在语言,关键字的可接受用途是什么,必须有一些否则为什么是c#语言中的
语句,或者这只是一个传统的东西来吸引来自其他语言的程序员习惯了什么?

我不能想到为什么你会使用它们的任何真正有效的理由,但也许是其他人有的。
I have never used a goto statement in my code, one of the first things I
was
told in my software classes a number of years ago was "goto statements are
evil and lead to spagetti code - do not use them".

I absorbed this mantra and tell this to other people and I fully believe
in
it. However, I was talking to someone recently and he was saying how much
he
loved goto statements and how useful they were and that Microsoft had to
leave them in c# because they were so useful, otherwise why would the
statement be in the language. He does not advocate using them in excess
but
uses them to allow himself to create a pattern that allows him to have one
point of exit in a function at the end. I kept trying to convince him
that
gotos were not good but he seemed unconvinced by my arguements.

I know one common use people say is for getting out of deeply nested
statements but you can easily do without gotos by using booleans or
replacing
sections of code with function calls.

Why if they are so bad is goto still in the language, what are the
acceptable uses of the keyword, there must be some otherwise why is the
statement in the c# language, or is this just a legacy thing to attract
programmers from other languages who are used to gotos?

I can''t think of any really valid reason why you would use them but maybe
other people have.




语言中的语句不是邪恶的。然而,一个声明如此微不足道的滥用是毫无意义的。使用goto是不值得的。所有我认识的开发人员都使用''goto''(并且只有少数几个)是预先构建的程序员首先是
。他们不想学习使用任何OO编程技术甚至是结构良好的b
编程的b $ b。我不能代表你的同事说话,但是根据你的描述,

论点是相似的。


不要浪费你的时间与这些争论乡亲。如果你对

问题有任何发言权,不要让他们设计任何重要的东西。他们想要在软件开发的后代中坐下来......让他们。你不是他们的守门员。如果你得到他们的代码,重构它。不要使用你的

方式,但如果你可以安全地删除它,不要在代码库中留下垃圾。换句话说,不要让自己被懒鬼和luddites挡住。

这对于职业生涯来说并不好。 />

我不能代表C#的设计师。我没有参与任何方式。在

我的观点中,goto被包含在内以便于移植现有代码,而不是用于帮助带来编写该代码的程序员的
。那些人很高兴能够留下来,而且我很乐意让他们留下来。


-

- - Nick Malik [微软]

MCSD,CFPS,认证Scrummaster
http://blogs.msdn.com/nickmalik


免责声明:本论坛中发表的意见均为我自己的意见,而不是

我的雇主的代表。

我不代表我的雇主回答问题。我只是一个帮助程序员的
程序员。

-



Statements in languages are not evil. However, a statement that so trivial
to misuse is pointless to use. Using a goto just isn''t worth it. All of
the developers I know that use ''goto'' (and there are only a handful) are
pre-structured programmers first and foremost. They have no desire to learn
to use any of the techniques of OO programming or even good structured
programming. I cannot speak for your co-worker, but from what you describe,
the arguments are similar.

Don''t waste your time arguing with these folks. If you have any say in the
matter, don''t let them design anything of importance either. They want to
sit in the backwaters of software development... let them. You are not
their keeper. If you get their code, refactor it. Don''t go our of your
way, but don''t leave junk in the code base if you can safely remove it. In
other words, don''t let yourself be held back by the slackers and luddites.
It''s not good for the ol'' career.

I cannot speak for the designers of C#. I was not involved in any way. In
my opinion, the goto was included to make it easy to port existing code, not
to help bring over the programmers who wrote that code. Those people are
happy to stay behind and I, for one, and happy to let them.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I''m just a
programmer helping programmers.
--




Mark R. Dawson <马********* @ discussions.microsoft.com>在留言中写道

新闻:F9 ********************************** @ microsof t.com ...

"Mark R. Dawson" <Ma*********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
我从来没有在我的代码中使用过goto语句,这是我几年前在我的软件类中告诉我的第一件事。是goto语句是邪恶的,导致spagetti代码 - 不要使用它们。
I have never used a goto statement in my code, one of the first things I
was
told in my software classes a number of years ago was "goto statements are
evil and lead to spagetti code - do not use them".




如果它们没有合法用途,编程语言就不会包括

他们。


我发现它们在编码算法中非常有用,这些算法本质上很大,而b $ b状态转换网络。每个代码块代表一个状态,并且当状态改变时,你会跳转到另一个代码块。


这不是一种常见的算法当然,但这是合法的

一个。当然,你必须要小心你的编码方式。


Gotos不能代替if-then-else块,for循环等等。 - 我

同意。


Gotos没有合法使用 - Falsch。


我们不想教授下意识的编程,程序员会自动做一件事,因为他们被告知,不是因为他们已经考虑了替代方案。

Michael Covington

人工智能中心副主任

佐治亚大学 - www.ai.uga.edu/mc


这篇关于真的很邪恶吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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