July 26, 2007

One of my pet peeves

As a programmer from the almost dark ages (my first PC work was on a TRS-80 and my first college class used punch cards), I've long considered the goto statement an abomination, one which too easily leads to ugly, unreadable code. It also allows the programmer to avoid thinking through a problem's resolution, since he always has the crutch to fall back on. I've even stated to my students that I would fail anyone who used one in a program. However, I will reluctantly admit that there are times when a considered goto statement can be useful. For example, you're been told to update some large production code to fix a a recurring bug. You wade through the program for days and finally find the error, deep inside multiple layers of nested conditions and loops. Do you (a), rewrite thousands of lines of production code so that you can gracefully back out of every level of recursion and iteration, or do you (b), add in an error label and insert a "goto error label" statement? Personally, I'd opt for (b). However, if I were writing the code from scratch, my answer would probably be different, as I'd have taken great pains to design my error handling routines properly. Not that you can make any program truly idiot proof; nature seems intent on upgrading idiots faster than I can upgrade my code.

Why do I mention this at length? There's a pretty interesting discussion over here at Kernel Trap. One of the participants in the discussion is Linus Torvalds, the father of all things Linux. He thinks that gotos are fine and dandy. I respectfully disagree. While they can, as I've mentioned, prove useful in a pinch, I think that they can prove a crutch which prevents programmers from properly considering a problem before moving directly onto the coding. And I disagree about his "they are often more readable than large amounts of indentation" comment. Then again, what they heck do I know?

Update: Speaking of programming, here's a nifty chart that diagrams the evolution of programming languages.

Update: I'm one of those morons of whom Ace speak regularly. I typed Linux Torvalds instead of Linus. Fixed now.

Posted by Physics Geek at July 26, 2007 12:44 PM | TrackBack StumbleUpon Toolbar Stumble It!

A minor point: his name is Linus not Linux.

Posted by: Steve L. at July 27, 2007 07:48 AM

I wrote my first program in Fortran IV back when I were a lad, around 1967. There weren't none of these fancy control structures, you used a computed GOTO and liked it!

Be that as it may, I've only used GOTO or its equivalent in assembly language since I started using structured languages. There've been a few times I could have used one, but the languages I was using didn't allow them.

For the most part, though, the higher-level control structures are just syntactic sugar; they just hide the GOTOs from the programmer. If you were disciplined, you could write code just as well using GOTOs. It's just that it's too easy/tempting to write spaghetti code unless the language helps you apply that discipline.

W.D. Maurer had an article back in the early days of BYTE magazine, "24 Ways to Write a Loop," which showed that many ways to organize the comparisons, IF..THENs, and GOTOs to provide looping structures. We get by on a lot fewer variants these days, because it's easier to develop solutions in terms of a few higher-level constructs rather than having to choose from a larger number of lower-level constructs that you'd have to remember how to implement yourself.

Posted by: wheels at July 27, 2007 08:18 AM

I was an assembly language programmer for a few years, so I'm well aware that BZ, BNZ and others are simply GOTO statements. I like the disguise of higher level languages because they help teach you to think the problem through, which most people won't do if they use the unconditional branch command.

Whoops on the name. I was reading Linus, thinking Linus, but somehow I typed Linux. I'll fix it.

Posted by: physics geek at July 27, 2007 01:39 PM

I still have some unused punch cards in case they come back in style.

In college, my "friends" would slip cards punched with "Woody Woodpecker" into my deck, which ended up giving me a dump with that name all through it. I didn't mind picking out those cards as much as having to go back to the computer center at 2:00 AM the next night to run and pickup my printouts for correcting.

Posted by: Woody at July 28, 2007 11:48 AM