Caring For The Code

I recently picked up the new book 97 Things Every Programmer Should Know edited by Kevlin Henney and published by O'Reilly.

In the currently-fashionable ADHD-afflicted style, It's a collection of 97 short essays on computer programming. I gently mock the concept but books laid out in this way are absolutely perfect for reading on an iPhone.

Anyway, I'm about a quarter of the way through and enjoying it a lot. The stand-out essay in the first quarter is by Robert C. Martin, entitled "The Boy Scout Rule". Here's an excerpt:

THE BOY SCOUTS HAVE A RULE: “Always leave the campground cleaner than you found it.” If you find a mess on the ground, you clean it up regardless of who might have made it. You intentionally improve the environment for the next group of campers.

What if we followed a similar rule in our code: “Always check a module in cleaner than when you checked it out”? Regardless of who the original author was, what if we always made some effort, no matter how small, to improve the module? What would be the result?

I think if we all followed that simple rule, we would see the end of the relentless deterioration of our software systems. Instead, our systems would gradually get better and better as they evolved. We would also see teams caring for the system as a whole, rather than just individuals caring for their own small part.

I don't think this rule is too much to ask. You don't have to make every module perfect before you check it in. You simply have to make it a little bit better than when you checked it out. Of course, this means that any code you add to a module must be clean. It also means that you clean up at least one other thing before you check the module back in. You might simply improve the name of one variable, or split one long function into two smaller functions. You might break a circular dependency, or add an interface to decouple policy from detail.

I really like this idea a lot. I've heard Wil Shipley articulate it as "clean as you go" - if you see something messy, fix it then and there.

You should definitely check out 97 Things Every Programmer Should Know. I bought it as a DRM-free ePub from O'Reilly (with the buy-one-get-one-free coupon code "BYGET" - don't know how long that's valid for). It reads just perfectly in Stanza on the iPhone.