Once upon a time, 1968, to be specific, the late great computer scientist Edsger Dijkstra, cautioned developers against using the goto statement. Specifically, he felt that goto statements make it hard to know exactly what’s occurred previously to any specific point in a program at runtime, which can make understanding, debugging, and refactoring source code more difficult. As he wrote in a letter to the editor published in the Communications of the ACM:
“The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one’s program.”
Were Dijkstra alive today, he would probably be happy with the results of a new study of how modern C programmers are using the goto statement. Based on those findings, C developers are still commonly using the goto statement, but are using it responsibly.
The study was done by a group of researchers from the U.S., Canada, Chile, and Japan and published this month under the title An empirical study of goto in C code. The idea was to see how often C programmers currently use goto statements and why they’re using them. To answer these questions, they looked at over more than 2 million files of C code from over 11,000 projects on GitHub. Out of this code, 384 functions were randomly chosen for a deep analysis of goto usage.
Some of their key results:
Use of goto in C is still common
26.6% of the projects examined used goto and half used gotos in 20% of their files. 11.4% of the C files examined had at least one goto statement, with 85% percent of those files having more than one goto statement.
Most functions that use goto use it sparingly and don't use it to do complicated things
25% of the randomly sampled functions used just one goto statement, while the median number used was three. The median number of lines of code in the block under a goto label was four.
Gotos are mostly commonly used for error handling and cleanup
Gotos are used in 80% of functions for error handling and 40% for cleanup. The authors argued that this makes sense, since C lacks the error handling and cleanup methods of other languages like Java and C++. Use of gotos for creating or exiting loops (which would be much more problematic) was much rarer, with 9% and 10% of functions doing so, respectively.
Gotos aren’t causing spaghetti code
Gotos in one code block under the label of a different goto (which can make it difficult to keep track where in the code a process is) happened in only 6% of functions.
Gotos aren’t being removed during post release bug fixes
In a close examination of six projects that had bug fixes merged into their release branches (which included GIMP and Postgresql) only 15 goto statements were removed during 1,900 post-release bug fixes. This suggests that gotos weren’t being viewed as the cause of the problems.
The big takeaway here is that, yes, C developers are using goto statements, but not in problematics ways. As the authors wrote,
“Thus goto statements do not appear to cause harm in practice, as they are mostly used for a limited set of straightforward purposes, quite unlike the ways Dijkstra feared they may be misused.”