English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Why does everyone hate goto so much ? It can shed lots of code when used correctly . I understand that overusing it can make for unreadable code, but having 1 single goto in a program can not cause any problems . I have seen people rewriting their code to be twice as long, just to avoid using a single goto !

2007-09-05 20:02:35 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

The problem concerns the overuse of GoTo statements and inadequate documentation.

It is not so much from the perspective of the original programmer, but from the perspective of others who must subsequently debug or modify the code. Code that is convulted due to GoTo statements can be difficult to modify or debug—because the normal flow of control has been altered. This sometimes makes the logic difficult to follow.

I only use GoTo statements as a last resort. If I can avoid the jumps by using relatively clear and concise coding, then I do so.

When I use GoTo statements, I always post liberal comments in each section that a jump lands.There, I itemize each occurence of a GoTo statement, along with the reason for its use; I also include excerpts of the codethat surrounds each GoTo statement. In that way, the user can easily refer back to a corresponding GoTo statement.

I truly do think that—in certain cases—the use of GoTo statements amounts to sloppy programming techniques. That is to say, they are used by some programmers to cover up for poor logic and lack of planning in program design.

______________--

2007-09-05 20:32:57 · answer #1 · answered by Einstein 5 · 1 0

Using goto for me has always been similar to running a sub-routine and I see no problem with using it. EDIT: Yes, with all the caveats below about writing understandable code (regardless of using goto or anything else) and yes, all coding should be adequately commented and documented.

The question is about using the best code for each task you want to accomplish - whether it is goto or something else should not matter. If goto is the best solution, then go for it!

2007-09-05 20:13:20 · answer #2 · answered by vbmica 7 · 1 0

goto is bad because you can not follow the program flow. If something wrong happen it's hard to you to debug. goto, continue, break are operations that breaks the flow control of your program. continue and break seem to be acceptable, while goto should not be used in practice at all. In the early stage of programming, there are not many options of control the follow (e.g if else switch loops), that's why people used goto. When the compiler are smarter, goto got obsoleted

2007-09-05 20:16:38 · answer #3 · answered by Anonymous · 0 1

fedest.com, questions and answers