DevZone Logo

Why 'goto' is Considered Harmful in Programming

FS

MD Fahid Sarker

Senior Software EngineerJuly 12, 2024


-
-
Share
Bookmark

Hello fellow coders! Today, we are diving into the mystical and often frowned upon world of the goto statement in programming. Even if you're new to coding, you've probably heard programmers make snarky comments about goto. So why is it so notorious? Let's take a step back in history and clamber through some messy code examples to understand why goto is considered harmful.

why bad

A Brief History Lesson

Once upon a time, in the early days of programming (circa the 1950s and 60s), the goto statement was widely used in languages like Assembly, Fortran, and even early versions of Basic. It allowed developers to jump from one part of code to another unconditionally. However, this led to the birth of the infamous "spaghetti code", a term used to describe code that is so convoluted and tangled that it becomes almost impossible to follow or debug.

In 1968, Edsger W. Dijkstra, a pioneer in computer science, penned the seminal paper "Go To Statement Considered Harmful," and the rest, as they say, is history.

The Challenge: Debug This 'goto' Mess

Before we delve into why goto is bad, let鈥檚 look at a challenge. Below is a piece of code using goto. Your job is to debug it.

Code.c
#include <stdio.h> int main() { int count = 0; start: if (count == 5) { goto end; } printf("Count is %d\n", count); count++; goto start; end: printf("Finished\n"); return 0; }

Well, that wasn't too bad, was it? Now imagine this code being part of a larger project with hundreds of goto statements. Yikes!

Why 'goto' is Bad

why bad

1. Readability and Maintainability

The primary reason goto is discouraged is that it makes code hard to read and maintain. Here鈥檚 an example to illustrate this:

Code.c
#include <stdio.h> void tangledLogic() { int x = 0; goto part2; part1: x += 2; goto end; part2: if (x == 0) goto part1; x *= 3; goto part3; part3: x -= 4; end: printf("Result: %d\n", x); } int main() { tangledLogic(); return 0; }

Good luck deciphering that on a Monday morning with only one cup of coffee! 馃憖

2. Error Handling

In languages that support structured programming, error handling is much cleaner and more straightforward without goto. Compare the following versions:

With goto:

Code.c
#include <stdio.h> int main() { FILE* file = fopen("example.txt", "r"); if (file == NULL) goto error; // Do something with the file // ... fclose(file); return 0; error: fprintf(stderr, "Error opening file\n"); return 1; }

Without goto:

Code.c
#include <stdio.h> int main() { FILE* file = fopen("example.txt", "r"); if (file == NULL) { fprintf(stderr, "Error opening file\n"); return 1; } // Do something with the file // ... fclose(file); return 0; }

Clean Code: Structured Programming

Now, let's refactor our initial challenge using structured programming constructs. Here鈥檚 a cleaner, more maintainable version:

Code.c
#include <stdio.h> int main() { int count = 0; while (count < 5) { printf("Count is %d\n", count); count++; } printf("Finished\n"); return 0; }

Isn鈥檛 that much more readable? Plus, future you鈥攐r anyone else maintaining your code鈥攚ill thank you for not making them untangle heaps of goto.

Conclusion

While goto is not inherently evil, the risks associated with its misuse far outweigh its benefits, particularly in modern programming where alternatives like loops and conditionals make the code more readable and maintainable. Leave goto in the annals of history and structure your code cleanly鈥攜ou'll have fewer headaches, and so will your fellow developers.

So next time you're tempted to sprinkle some goto magic in your code, think of Dijkstra and give structured programming a warm hug instead.

why bad

Happy coding and keep it clean! 馃槃

Found the blog helpful? Consider sharing it with your friends.
Buy Me a Coffee

GotoProgrammingBad PracticesDebuggingHistoryCC++

Related Blogs

Blogs that you might find interesting based on this blog.