Home > Development, Rants > Clean Code

Clean Code

November 21st, 2005 Leave a comment Go to comments

We who cut mere stones must always be envisioning cathedrals.
-Quarry worker’s creed

That quote comes straight out of one of my favorite books: The Pragmatic Programmer. Ever since I stumbled on this book four plus years ago I have been an evangelist for its teachings. I read the author’s blogs (PragDave, and Andy’s Blog) religiously and I keep hoping the Rails Studio will make a detour to the Midwest.

Anyway, one of my absolute favorite pearls of wisdom from Pragmatic is “Don’t Live with Broken Windows.” Basically, a car in a seedy part of town will be left untouched until a single window is broken – then it’ll be stripped in a matter of hours. For more on the theory, check out the Wikipedia entry: Fixing Broken Windows.

This theory was brought to my attention today in a code review. Not that the code was bad (though truthfully, I don’t know enough about the business yet to be able to do more than “that variable name isn’t very descriptive”) but it had some dead code in it. I’ll admit, there was a time (a brief while) when I didn’t mind having lines and lines of commented out code but not anymore. With version control (you do use version control right?) there just aren’t any good reasons to keep unused code around. All you’re doing is making the code harder to read – and most of us prefer writing code anyway! A simple thing, sure, but next time you’re editing some source and you run into a chunk of code that the compiler is ignoring do yourself and everyone else in your shop a favor and burry that code for good. You can always look back at the history in your version control…

Update: I knew there was something else that made me think of this topic recently. Check out Anatomy of Ugly: How Good Code Goes Bad. This is what happens when you read way too many blogs and newsletters (or so my wife says).

Categories: Development, Rants Tags:
  1. Joe
    November 27th, 2005 at 18:18 | #1

    I agree with you about dead code. If its not being used, you are just cluttering the program and making things more difficult. However, I have stumbled upon this several times in code that always makes me pause when i want to delete it. It almost always looks like this:

    //Here’s something that we should turn on later when xyz happens…

    Now I have no idea what this code is referring to any more. In my mind if this code should be functioning why isn’t it uncommented? However, how do I bring myself to delete something that obviously was meant to serve a purpose? No one is ever going to go back into source code control to retrieve something that they don’t even know exists. What do you think about this?

  2. November 28th, 2005 at 21:07 | #2

    I think you are talking about the prototypicall TODO comment. To me, it’s one thing to leave yourself some reminders but to comment out code that truly isn’t functioning anymore is just setting yourself up for pain. That said, I have to consider the YAGNI principle (http://xp.c2.com/YouArentGonnaNeedIt.html) – when you write a comment like that you might *think* you will need to turn this on, but how often have you actually done it? You hit it right on the head when you said “Now I have no idea what this code is referring to any more.” Exactly. You did when you wrote it…but that was two months (OK two days) ago. Frankly, I think clean code that is easily modified will provide greater benefit than you *might* get from a bread-crumb like that.

  1. No trackbacks yet.