Template:Computer Programming/Control/5
Is goto evil?
[edit source]Since Professor Dikstra wrote his article Go To Statement Considered Harmful, goto is considered bad practice in structured programming languages, and, in fact, many programming style guides forbid the use of the goto statement. But it is often overlooked that any return which is not the last statement inside a procedure or function is also an unconditional statement — a goto in disguise. There is an important difference though: a return is a forward only use of goto. Exceptions are also a type of goto statement; and note that they need not specify where they are going to.
Therefore, if you have functions and procedures with more than one return statement you are breaking the structure of the program similarly to a use of goto. In practice, nearly every programmer is familiar with a 'return' statement and its associated behavior; thus, when it comes down to readability the following two samples are almost the same:
Note also that the goto statement in Ada is safer than in other languages, since it doesn't allow you to transfer control:
- outside a body;
- between alternatives of a case statement, if statement, or select statement;
- between different exception handlers; or from an exception handler of a handled_sequence_of_statements back to its sequence_of_statements.