TechTorch

Location:HOME > Technology > content

Technology

Goto in Programming: When is it Ever Acceptable?

January 28, 2025Technology1298
When Should I Not Use a Goto? Always. But you need to understand WHY g

When Should I Not Use a Goto?

Always. But you need to understand WHY goto should not be used.

Why Use GOTO when You Can Use the Much More Versatile “COME FROM”?

INTERCAL is a programming language that many coders choose to write as a form of programming ritual. You will never have the urge to write a GOTO again if you write some INTERCAL code.

ALWAYS

I have never seen a situation in any modern programming language where a GOTO is ever needed. There are far better alternatives:

In loops - either break or continue are the right methods to use. For exception handling - many languages have a try/except or catch/except type structure.

NEVER

Use goto except when your language offers you no reasonable alternative. A well-designed language like Python simply doesn’t have a goto statement, so you can’t use it.

The only time I even slightly feel the need for it while using Python is in case 3 below. If you look at the things which Python has and your language lacks, this will give you a clue as to when you must use a goto to get around your language’s deficiencies.

When to Use GOTO

If you can’t easily handle exceptions using: try: ... except: ... finally: ... you may have to use a goto to transfer out of a block of code to another block of code that tidies up after the exceptional state is detected. If you don’t have a defined way for breaking out of a loop (e.g., while True: ... if condition: break) then you have to use a goto to transfer control to a label after the end of the loop. If there are several nested loops and you want to break out of all of them, a goto to a label after the end of the outer one is acceptable. A single goto and one label is better than one for each level of looping with flag variables. If you don’t have a defined way to abandon an iteration and immediately start the next one, Python's continue, then you can use a goto to a label immediately. In simple cases, it is better to make the rest of the body of the loop a conditional if block, but not if there are many reasons for skipping to the next iteration, where the alternative is an excessive depth of nested conditionals. Quite a few would disagree with me on this.

Using goto can lead to complex and difficult-to-understand code, decreasing readability and maintainability. It is generally considered a best practice to avoid goto, even in languages that support it, as the alternatives often provide clearer and more maintainable code.

Conclusion

In summary, while goto is a powerful control flow statement, it should generally be avoided in favor of more structured and readable alternatives. Always seek out and use the most appropriate control flow tools provided by the programming language, such as loops, conditions, and exception handling, to write cleaner and more maintainable code.