Useless code
andy on May 8th 2007
When contracting, I get to see lots of code. Some of it is good, some of it bad, and some just leaves me scratching my head.
For example, I came across this today:
//ASSERT(1);
Apparently things had gotten so bad for this programmer that he needed to make sure 1 still evaluated to true. I’ve had days like that. Better yet, when done, he just commented the line out, helpfully allowing future developers to quickly make use of his wisdom.
Such a philanthropist.
Filed in Amusing, Contracting, Programming | 11 responses so far
11 Responses to “Useless code”

Tom May 8th 2007 at 05:59 pm 1
If this amuses you, check out thedailywtf.com
Andy May 8th 2007 at 06:07 pm 2
Oh, I have Worse Than Failure (the new name) in my RSS feed.
Joe Goh May 8th 2007 at 06:25 pm 3
There could be another reason why he chose to do ASSERT(1) – to make sure that assertions are not compiled in the release build, or some other reason where he would want assertions to trigger everytime. Is this ASSERT(1) in the -init method?
The good news though is that at least he uses ASSERT(). I’ve known quiite a few coders in the past who have not even heard of the concept.
Andy May 8th 2007 at 07:08 pm 4
Well, ASSERT(1) would _never_ fire, where had he done ASSERT(0), that would fire all the time.
And like you said, ASSERTs that fire all the time can be useful (i.e. if you reach a code flow that shouldn’t ever be reached).
Joe Goh May 8th 2007 at 07:34 pm 5
Ooops, that’s right. Serves me right for replying to blogs 10 mins after waking up.
Hmm, in that case, I have no idea why he wrote that line. If the original coder is still around, perhaps you should ask him (nicely).
George Sudarkoff May 8th 2007 at 10:02 pm 6
We all have our own little moments (and idiosyncrasies). Sometimes, especially when you are chasing after a particularly illusive bug, such small reassurances of things still being generally sane might help you not to loose it completely.
Or maybe it’s a reminder for something. Or a case of careless find and replace. Or a result of some script running. Or this is a custom ASSERT that does more than you think. I mean, who knows? Don’t be so quick to judge.
Joe Goh May 8th 2007 at 10:21 pm 7
In my previous full-time job before this startup, i’ve seen quite a few scary bits of useless code too.
else clauses with no code inside.
switch-case statements with LOTS of unreachable code following a break statement, which makes you really, really scared. Where is there loads of code in a switch-case in the first place? Ahhh, that’s a much longer story for another day.
Lots of “legacy functions” that nothing is calling anymore. And these functions are HUGE (more than 150 lines for sure).
Life as a maintenance programmer is good.
Joe Goh May 8th 2007 at 10:23 pm 8
“Where is there loads of code in a switch-case in the first place?”
Oops, should be “Why” instead of “Where”.
George Sudarkoff May 8th 2007 at 10:28 pm 9
You want to see HUGE functions? Look at this: http://george.sudarkoff.com/2007/02/_how_many_problems_can.html
Jonathan Grynspan May 11th 2007 at 08:14 pm 10
It’s possible that, at some point in the development cycle, a complex expression was in the place of that “1″, as well as in other places. As the program evolved, the expression became obsolete and a clever developer decided to replace all instances with “1″ to avoid having to modify logic or redo unit testing.
It’s a bit of a stretch, but one can hope that the developer was not so blatantly dumb.
Uli Kusterer May 22nd 2007 at 02:20 am 11
Maybe the programmer just needed a place to set a breakpoint on? This is essentially a no-op, but it still compiles to something. If this is a place where he needs to break often during debugging (i.e. it was some sort of main entry point or bottleneck), he’ll be happy if he can just uncomment this thing, put a breakpoint next to it and get debugging.
And maybe one programmer put this code there, and another came by, thought: “What’s the point of this? I’ll comment it out and ask Jeff why he did that!” And that’s how it happened. If nobody had to debug this spot recently, it may have just not been uncommented again.
I could see dozens of reasons for such a statement, all perfectly valid.