
GOTO was considered harmful 50 years ago
TL;DR: Don't ever use GoTo.
Problems
Readability
Hard to follow code
Solutions
Replace GOTO with structured code
Use exceptions
Context
I started programming in Basic.
GOTO was heavily abused there.
I had to learn structured programming from scratch in Rehab mode.
Sample Code
Wrong
for x < 0 {
if x > -1e-09 {
goto small
}
z = z / x
x = x + 1
}
for x < 2 {
if x < 1e-09 {
goto small
}
z = z / x
x = x + 1
}
if x == 2 {
return z
}
x = x - 2
p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gamP[5])*x + _gamP[6]
q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_gamQ[5])*x+_gamQ[6])*x + _gamQ[7]
return z * p / q
small:
if x == 0 {
return Inf(1)
}
return z / ((1 + Euler*x) * x)
}
Right
for x < 0 {
if x > -1e-09 {
return small(x, z)
}
z = z / x
x = x + 1
}
for x < 2 {
if x < 1e-09 {
return small(x, z)
}
z = z / x
x = x + 1
}
if x == 2 {
return z
}
x = x - 2
p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gamP[5])*x + _gamP[6]
q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_gamQ[5])*x+_gamQ[6])*x + _gamQ[7]
return z * p / q
small(x, z){
if x == 0 {
return Inf(1)
}
return z / ((1 + Euler*x) * x)
}
}
Detection
[X] Automatic
In languages supporting GOTO, our linters can warn us against its usage.
Tags
- Readability
Conclusion
We acknowledged GOTO problems a few decades ago.
The problem is still present in modern languages.
Most programmers luckily avoid GOTO sentence. Next goal will be to consider harmful null usage.

Courtesy XKCD
Relations
More Info
Credits
Photo by Jens Johnsson on Unsplash
It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.
Edsger Dijkstra
This article is part of the CodeSmell Series.
About the author
I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.
Comments
Join the discussionSolutions
- Replace GOTO with structured code
- Use exceptions
Imho exceptions aren't much better. They are essentially a goto where
- you don't know where you're going,
- you can send data with it (including context) and
- you can guarantee execution of code if needed (I've never really worked with
gotos but I assume there is no equivalent tofinally).
2 & 3 are nice and probably the reason why they are still used today, but 1 is terrible for understanding bigger systems.
Code Smells
In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.





