Viewing a single comment thread. View all comments

Dunbaratu t1_iyatoyo wrote

First you have to understand how it was originally used, then understand what replaced it, then you can understand why it's not used anymore.

The ability to skip ahead or skip back to a different line of code lets you do a great many things. It's generic and open-ended what it can do. When you create a way to do it conditionally where it won't always execute the goto then it allows for all the standard flows we use today.

An If-block is really just "If this condition isn't true then GOTO the spot just past this block so you skip over it."

An unconditional loop is really just "GOTO a previous line up above so you start executing all this stuff again. When you get back down to this line, GOTO the top again. Repeat."

A conditional loop is really just "IF the thing we're checking for to end the loop isn't true, then GOTO a previous line and run that stuff again."

It's versatile. It can form just about any pattern.

So what's the problem? Well that versatility IS the problem. To understand why a programmer put a GOTO there, to understand what they were trying to do, you have to analyze a lot more lines of code to see what the pattern was. Because a GOTO could really be for a wide variety of purposes. It might be part of an IF. It might be part of a WHILE (thing is true) loop. It might be part of an UNTIL (thing is false) loop. It might be part of a counting loop (FOR loop). It might be to just jump over to a subroutine and return from it.

It's not visually obvious from just looking at it what it's for. You have to trace the code line by line before you can see the big zoomed-out picture of the layout.

It also allowed unique flow patterns that don't fit into one of the commonly known patterns. A clever programmer could have been using it to do something unique that only makes sense in their own mind and doesn't make sense to anyone else.

So, essentially, it can make the program hard to understand, which then means programmers make mistakes when editing it.

The fix was to find all the common things people were using GOTO to do, and create special keywords for those patterns and replace the use of GOTO with those new terms. So you can still do all the same stuff, but in a more descriptive way that actually says what you're doing. You don't have to guess "Which of the 8 or 9 different reasons for using a GOTO might this one be?" when it uses a word like WHILE or FOR or IF that actually tells you which one it is.

But the interesting thing is... under the hood, it's still really all GOTO. At the lower machine language level, you have the various JUMP instructions, which are exactly what a "GOTO" would have compiled into, and as it turns out, is also what the newer words like WHILE and FOR and IF also still compile into.

Under the hood, it's still all GOTO. But now it has more descriptive ways to summarize those GOTOs into human-readable structures.

16