Thursday, September 21, 2006

Goto's are evil

OK, so I'm working on some legacy code today when I stumble on a compiler warning: "C4102: unreferenced label". The cogs in my brain started ticking over..."label, label, hmm, what could that be"? Then I took a look at the code. Oh crap. Goto's. In C++. In one function (~100 lines) there were 21 goto calls.

Practically unmaintainable.

Lucky the author wasn't still with the company otherwise I'd have given him an earful.

Mr Duffy you suck.


Jeremy said...

You brain washed idiot.

It isn't the gotos that are the problem. It is your lack of ability to understand the code written by the other developer (who probably got fed up with tired non-grounded arguments such as yours and left for pastures greener).

If you don't know what a 'label' is then you shouldn't be programming.

"I can lay bricks - but I don't pretend I can build a whole house!"

Anonymous said...

You brain washed idiot.
Goto IS a disastrous problem, because it tears out a big hole in the high-level language.
* It doesn't create a block, so the variables aren't local in the "goto-block".
* It's always a sign of bad programmer, because it almost always can be coded usign higher-level structures.
* Some people use to say that goto is good for breaking nested loops. Yes, maybe in C, but not in C++. They forgot about the fact that goto jumps over the deconstructors' calls and it may lead to real disaster [memory leaks, unreleased resources, data loss etc.].

Anonymous said...

You may want to take a look at this thread.

Matt said...

@ Jeremy: Throwing names at people is a great way to bolster your argument. Douchebag. There was no lack of understanding, the concept of labels is, after all, very simple. No. What I suffered was a mild case of disbelief that someone would write such poor code. Regardless whether you feel that goto's are "evil" (for the record, in C++, I do) code should not be difficult to read. This code was and the goto's were exacerbating the problem.

@ Anonymous #1: Yep, I agree with pretty much all of that. I'm willing to concede that they have a place in C (a very small, ugly little place) but in C++ I've yet to come across a compelling reason to use them. In every example I've come across they've impacted the readability of the code.

@ Anonymous #2: Thanks for the link; I've read that thread before. It's a very popular link for goto-evangelists to pass around. I don't strenuously disagree with most of what's said there (though Linus' arguments are pretty weak) but keep in mind that it's discussing C code. As I said above there are some valid reasons for preferring using goto's in C. For example you'll note that the vast majority of goto's in the Linux kernel are for error handling. Modern languages, including C++, would use exceptions which are a more elegant solution for this problem.

Anyway, while it's interesting to discuss the evils of goto, is anyone out there seriously going to try to defend 20+ goto's in a single function?!