Published on Sep 1, 2010

Why is using GOTO statement bad programming practice

Recently, I looked over newest changes in latest release PHP 5.3.2 and astonished noted amongst others introduction of the GOTO statement available since PHP 5.3.

Recalling all my experience since early '90, when I used to poke around old good ZX Spectrum Sinclair Z80 computer and learned first basics of programming, I would like to explain why I consider generally using the GOTO statements in programming languages for contraproductive and one of the worst programming practices.

First of all - it's extremly easy to get used to. All newbies to programming find the GOTO control structure easy to grasp. Hence, it will be one of the first control structures to start using. There is almost no learning curve required, no forcing to think structurally.

Second - The GOTO statements in my own experience used to be one of the biggest source of code crashes, bugs and hard-to catch jumps. I remember, writing a program over 200 lines of code in BASIC was almost guaranteed it will crash sooner or later due to some missing variable skipped via GOTO statement or any other inconsistency. It si okay to use GOTO in small routines, because they are usualy easy to maintain. It is okay to use it for teaching purposes to ease learning curve for beginners. However, it is not okay to use GOTO statements within application with hudreds lines of the code. All in all, GOTO goes against all principles of procedural and object oriented programming.

Fig. 1 - Perfect solution: use procedure rather than GOTO !

And once again my experience: when I moved from Sinclair BASIC to (also ancient) Turbo Pascal with more procedural approach, I was astonished, that my code became significantly more stable. I could write programs featuring even hundreds lines of code - and it just worked! Since then I consider programming with strong emphasise onto procedural (OOP) approach for the right way unlike controlling execution flow via the GOTO statements. Here, I recall quote of some famous programmer: "Instability of your code grows lineary with using GOTO statements.". I totally agree.

On one hand - I understand that from philosophical point of view, introducing GOTO statements goes along with the PHP philosophy - to give folks easy tool for fast and simple web development.

On the other hand - there has been lots of discussion on using the GOTO statements around the community which more or less proved that implementing GOTO control structure encourages producing hard to maintain code, or in other words the code with significantly higher maintenance costs.

Personally I consider GOTO controls structures to be perfect example of bad control design patterns and should not be supported in any programming language. Instead, forcing practices with code stability in mind should be preferred option, even if the learning curve is more demanding for beginners.

Actually, I am wondering what's behind - why a programming language with such a great improvements in other areas (OOP, XML, ..) moved a slight step back:-)

Finally my recommendation to everybody - try using GOTO heavily and sooner or later you'll find yourself rewriting large parts of your code:-)

Links on discussions about using the GOTO statements in programming languages:

Got a question?

Professional development of web applications and custom solutions. Consultancy services.