“Hello, I’m a Slave” “And I’m a PC” About The Intel Threading Tools, Our SIP Stack, and Me

 
Ran Arad

Five Bad Medicines for a Sick Code

Categories: Development
March 4th, 2009

I’m now maintaining and developing code that was out of my hands for a long time, and I found an interesting phenomenon I call “bad medicine”. The developer in charge of the code while I was gone knew something was wrong, but could not figure out how to fix it. So he added a crutch here and a splint there to keep the sick code “breathing” and quickly moved along.

Let me rant a bit about my experience with Programmer Joe and his bad medicine.

1. An Unhealthy Dosage: Code replication

Why, I wondered, would Programmer Joe copy a large code piece only to do some minor changes in it? Only half way through refactoring the code, I realized the answer: Old P. J. didn’t understand the code enough to be fix it inline, so he just copied it and tweaked it until it worked the way he wanted. Nice work, Joe. Customers  love increased footprint and a healthy dosage of repeated bugs.

2. The Splint: Data out of place

There were definitions inside the code file that were moved to the header file. That was weird, even for Joe. These weren’t functions made public, you see, these were data, large arrays of data, defined as “static const” and moved to the header file. Again, I figured the footprint-conscience customers must love Joe, but why, why, why?! I moved the data arrays back to the code where they belong, and recompiled to reveal the errors – in our sample code. Now, if your own sample code needs you to move data to the header file, you know something is wrong, but you should also know that what you are missing is an access function. Actually, there was an access function, only Joe didn’t know it.

3. The Crutch: Constants from heaven

Knowing what arguments to pass to some function can be tricky. How lucky was Joe, then, to find such a handy definition:

#define ARBITRARY_NUMBER (5)

Ah, wait – it was the other way around, wasn’t it? This time Joe felt responsible enough to make it obvious, other times constants just appear out of thin air. Joe couldn’t be bothered to find the real value to pass, so he found a value that works – most of the time, at least. Now, every time I see some obscure definition out of place an alarm goes off in the back of my head.

BTW, Joe – 42 is the REAL answer to the universe problems.

4. Placebo: Errors firing the wrong way

Repeatedly in programmer Joe’s code, error codes were returned for various reasons: Data was not there? Return an error. Wrong state? Return an error. Don’t know what to do? Unexpected input? Feeling sad? Return an error. Not always wrong, of course, but actually attempting to handle the problem might work better.

What’s worse, is P.J. returning an error to a caller that’s not interested: a timer pops, the function encounters an error, so Joe decides to return an error. The timer engine doesn’t care, of course, so the error is never handled. An attempt to do something, even if it’s notifying the layer above or even log the error, would have worked much better.

5. Prayer and Positive Thinking: Holy code

Joe finished his sacred work and felt much better; the sanctity of the code was not harmed. He only had to duplicate some data, and reverse half of what the “holy code” did, but the code itself was not touched. Joe said another little prayer for the health of whoever wrote the code years back and went on with his life.

Holy code is the hardest to catch, but it’s really worth the while. You have to look back through the versions and see what code was left untouched – no code is that good. It is more likely that that piece of code was sanctified, and any bug in it was undone before or after it.

So are YOU Programmer Joe too?

If you’re given someone else’s code, and you need to maintain it, please: MAKE SURE YOU KNOW WHAT YOU’RE DOING BEFORE YOU START MUCKING AROUND!

2

Comments and trackbacks

  • 1. Katy Jo Franklin  |  November 15th, 2009 at 4:17 pm

    Thanks for an informative article on coding. It is so true in most cases today. In the guise of eliminating expenses for a company, many companies are trying to “buy” expert software that is already coded.

    This is a huge mistake and a disaster waiting to happen. Coding is almost never generalized “plug and play”, but had to be adapted to the corporation business processes.

    Paying for experience and knowledge (most of the time learned by shooting onesefl in the foot) is a justifyable when we talk about recovery of loss data and be able to stay up with current data file extensions.

  • 2. Ran Arad  |  November 17th, 2009 at 9:37 am

    @Katy
    You do realize that selling “expert software that is already coded” is what we do here it RADVISION? and that we generally think it’s a great idea to actually buy “experience and knowledge” from us?
    What I wrote about is various problems I found during the years in code developed internally, not externally. I believe these are problems caused by under-trained or under-capable programmers, and sadly, you can never really escape them, no matter what you do.

Required

Required, hidden

Notify me of followup comments via e-mail

Trackback this post  |  Subscribe to the comments via RSS Feed