7.23.2010

other people's code.

The first real world code that I ever wrote was on the flagship product at my old job. You remember, the one where I wrote software that talked to trucks. I was given a relatively smallish task: Add a column to a chart full of truck information, and fill in the appropriate info into that column. It was a good assignment for a newbie in that it was fairly representative of the kind of work I've done in the real world. In college, you write small applications from scratch. In the real world, you write code to add functionality to an existing product. In college, the whole program is designed and written by you. In the real world, your new chunk is designed and written by you, but all that other code was designed and written by other people. Not one other person, lots of them. The older an application gets, the more fingerprints it has on it. When you add your own fingerprints, you have to work with the design and code that is already there. In college, you write code. In the real world, you shoehorn it.

Sometimes, this is okay. I've had mostly good experiences with other people's code, because the majority of people that I've worked with have been competant programmers. Some of them are fantastic programmers, and it's frequently a pleasure to add my own contribution. Because what they wrote was designed well in the first place, it is easy to add on. Other times, it is not okay. When Sartre said that hell is other people, he had not yet met the hell that is other people's code.

Anyway, back to that first assignment of mine. I did an okay job, I guess. It worked pretty well and there were pretty icons, which seemed to make people happy. There were bugs, but there will always be bugs.

A year later, I was asked to fix one of those bugs. I jumped back into that code that I had written, and I found it very difficult to figure out just what was going on. It was downright confusing. That was my first experience with code amnesia. I could tell it was my code, because it looked like my style. And I had a vague memory of writing the feature. But I had no recollection of how it worked. This has happened several times since then. Spend six months away from some code that you've written, and you'll have no idea what it does anymore. It's as if a novelist forgot his characters' names six months after finishing the book.

Having spent some time away from that first assignment and then being thrust back into it, I noticed some glaring design problems. Namely, that it was a really crappy design. It was unnecessarily complex, when it could have been much more simple and elegant. I'm not sure if I was able to realize it was bad design because I was coming at it with fresh eyes, or if I had grown so much as a programmer in that first year of real world coding that I had actually learned something about how not to write bad code. What it amounted to in the end was that I had written code that would be hard to maintain. If even I had a hard time following the design well enough to fix a bug (and remember, I wrote it), then the poor fellow who had to fix the bugs after I left the company was in worse shape. Hell was my code. It was a very humbling experience.

Only the programmers among you will understand this part, so I only include it as an aside: there was a hashtable of arrays. There might have been more than one. While there are appropriate times to use such a thing, it should be a red flag.

This week, in my real world job, I had to fix a bug in a feature written by someone else. And the deeper I got into this code, the more I suspected that it just wasn't a very good design. Now, I wasn't here when it was written, so I don't know the requirements. I was thinking about maybe rewriting a couple of parts in it, just small parts, to make it a little less, you know, crashy.

And then I saw the hashtables of arrays. There were ten of them. TEN. I wanted to cry. Or have a beer. Or cry in my beer. Working with this code feels like penance for the bad code that I wrote, that some poor sop now has to maintain. I don't know what he's doing penance for. I can't even feel superior as I try to fix this bad code, because I know that I once wrote something just as bad.

The worst part is, I could still be writing bad code now. Maybe this is why people in this industry switch jobs so often.

No comments: