Out of the corner of my eye

Exploring HumanStateSpace

Out of the corner of my eye header image 2

Response to “signs you’re a crappy programmer”

December 2nd, 2007 · No Comments

Damien Katz blogged some “signs you’re a crappy programmer (and don’t know it)”, and the article ended up on the front page of Digg. Many of the comments criticised some of what he said, but I didn’t anywhere find what I felt should be the strongest criticism of “You think no function/method should have multiple return points.”

A lot of people vaguely mentioned it makes functions harder to debug, but weren’t very specific. In case anyone is still undecided on this, I’ll tell you about one of my experiences trying to debug such a function.

It was when I was working in the games industry. Video games these days when they come out are expected to have 0 bugs and 0 memory leaks. Probably an unobtainable goal, but most devs work pretty damn hard towards this target, particularly the memory leaks part. This is because consoles don’t have anywhere near as much memory as high end gaming PC’s - you just can’t afford to waste any memory.

The function in question was quite a large one (another thing Damien states is perfectly fine). I can’t remember exactly what it did, but I do remember at one point it implicitly allocated memory to play a sound. Then about a page further down, there was an “early out” condition. Finally, at the end of the function, the memory was freed.

It took me quite a few hours to nail the memory leak. It wasn’t immediately obvious the memory was being allocated, and more importantly, it was only after very carefully examining every line of the function that I even realised there was an early out - just that one little “return”, hiding amongst a massive set of complicated conditional statements.

Someone on Damien’s blog post commented that “what’s the problem, your IDE highlights return anyway, you can’t miss it”. If I remember, I did have syntax highlighting on, but return wasn’t one of the keywords highlighted unfortunately (we used VI, not Visual Studio).

Much time would have been saved if:

1) early outs hadn’t been used
2) the function had been refactored so it wasn’t so long

To further drive this point home, my lead at the time had already told me about how he doesn’t like people using early outs and recommends we try to avoid them if possible. Obviously it’s a recommendation, but a good one I believe.

Tags: C++ dev

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment