开发者

Why is not advisable to use goto frequently? [duplicate]

开发者 https://www.devze.com 2023-01-02 16:40 出处:网络
This question already has answers here: 开发者_JS百科 Closed 12 years ago. Possible Duplicate: GOTO still considered harmful?
This question already has answers here: 开发者_JS百科 Closed 12 years ago.

Possible Duplicate:

GOTO still considered harmful?

I have read articles that advise on refraining from using the goto operator frequently, but never told why. I have googled this for ever and can't really find anything.


GOTO was often used frequently in situations that produced "spaghetti" code. In other words, to follow the logic of the code required you to move frequently from point to point, often in an unstructured or unobvious way. This lead to code that was difficult to understand.

But in the old days (early BASIC), GOTO was often the only way to do iteration so there wasn't always a choice. More expressive languages provided other ways of doing iteration (for, do ... until, while and so forth) where the loop was far more obvious.

Having said that, it's really the overuse of GOTO that's considered harmful. It still has its place in many situations such as error handling or finite state machines, provided it doesn't make the code too hard to follow.


Search for "Goto considered harmful", this is a paper written by Egster Dijkstra (and published by Niklaus Wirth which gave it its title), which discusses the why.

this paper is now part of the computing culture, a piece of history and is a must read for people interested in computer history (and any good programmer should be interested in such a topic)


http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF

But of course you'd better be rational, and do not exclude goto completely. It has a number of perfectly justified uses.

0

精彩评论

暂无评论...
验证码 换一张
取 消