Goto Is Your Friend

So you mean to tell me I should use GOTOYes Sir, that's what I'm gonna say!

I've started coding when I was 13. In Basic. And no, I'm not gonna say "good old times". Going back to those days, the only things I remember are the command AUTO to automatically create line numbers1 and the (ab)use of the GOTO statement.

Few years later, at the high school, my computer science teacher told me that goto is evil but luckily its use can be always avoided (see the Structured Program Theorem). One of the reasons for its bad reputation is that usually the code that uses goto is less readable and the program flow is difficult to follow. In two words: spaghetti code.

Only a couple of years ago I realized that this is not true. goto is not evil by itself and in some (many) situations its use can produce a code that is more readable, more optimized, and a with smaller memory footprint.

Do you want some examples? How do you exit from multiple nested loops? Without using goto you should have at least a variable and an if for every loop that encloses loops and then you should have a bunch of breaks. And you know what break is, right? A masked goto.

And what about multiple exit conditions that need to free allocated memory? Without using goto, you should duplicate the cleanup code with the risk to forget something every time you need to modify the code in the future.

These are only a couple of examples about the usefulness of the goto statement. If you are not fully convinced, have a look to this thread.

  1. I've started coding on a Olivetti PC 128S that ran an operating system loaded from ROM. The OS was written in BASIC and the interpreter was available at user space too. Unfortunately the embedded editor was really poor, so, the fastest way to create a program was to directly type instructions in the command line, preceded by the row number. The command AUTO automatically added multiples of ten at the begin of each row.

Image created by me with

Post last updated on 2017/06/25.

Luca Sommacal

Luca Sommacal

Italian developer (mainly in C for embedded platforms), Linux learner, addicted to rock music, history, science and few other things. Follow me on Twitter

comments powered by Disqus