Goto considered harmful

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(New page: This is not particularly relevant to higher level languages such as Java which do not have goto statements (note that in Java "goto" is a reserved keyword but is not used), but in low leve...)
 
m (Learned how to link in MediaWiki)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This is not particularly relevant to higher level languages such as Java which do not have goto statements (note that in Java "goto" is a reserved keyword but is not used), but in low level languages such as Fortran goto statements are considered bad practice.
+
This is not particularly relevant to higher level languages such as Java which do not have goto statements (note that in Java "goto" is a reserved keyword but is not used), but in low level languages such as Fortran and C goto statements are considered bad practice.
  
 
This is because, simply put, goto statements make the behaviour of a program harder to understand. To quote Dijkstra "...the quality of programmers is a decreasing function of the density of go to statements in the programs they produce." The harder a program is the understand, the harder it is to maintain.
 
This is because, simply put, goto statements make the behaviour of a program harder to understand. To quote Dijkstra "...the quality of programmers is a decreasing function of the density of go to statements in the programs they produce." The harder a program is the understand, the harder it is to maintain.
 +
 +
The excessive use of goto statements can use to very convoluted code with a complicated flow of control. Such code is sometimes called [[Spaghetti code]]
 +
 +
In most cases where it becomes tempting to use a goto statement a more elegant solution is to break the method in question into several submethods.
 +
 +
== See also ==
 +
* [[Design maxims]]
 +
* [[Spaghetti code]]
 +
 +
== Links ==
 +
* [http://userweb.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF Go To Statement Considered Harmful, E. Dijkstra]

Latest revision as of 03:39, 16 July 2010

This is not particularly relevant to higher level languages such as Java which do not have goto statements (note that in Java "goto" is a reserved keyword but is not used), but in low level languages such as Fortran and C goto statements are considered bad practice.

This is because, simply put, goto statements make the behaviour of a program harder to understand. To quote Dijkstra "...the quality of programmers is a decreasing function of the density of go to statements in the programs they produce." The harder a program is the understand, the harder it is to maintain.

The excessive use of goto statements can use to very convoluted code with a complicated flow of control. Such code is sometimes called Spaghetti code

In most cases where it becomes tempting to use a goto statement a more elegant solution is to break the method in question into several submethods.

See also

Links

Personal tools