Why Write Code When You Can Remove Some?

by Ville Laurikari on Thursday, August 20, 2009

Cutting out some unused bits.

Some of the most satisfying commits I’ve ever done were the ones where I got to remove a lot of old cruft.

As your codebase grows older, it will accrue technical debt. It comes in many forms, but sometimes the most straightforward cure is to cut the bad parts out.

I’m not talking just about dead code. I’m talking about obsolete code. Obsolete code is code that was there for a reason, but the reason is no longer relevant. They are the appendixes of software – still there, still alive, but apparently not serving any useful function. Cut it out before it gets infected.

There are some simple tricks and practices to keep tabs on obsolete code. One of my favorites is the XXX comment:


// XXX (tag1, tag2): Description

You know, like

// XXX (obsolete): When 2.0 is released, this won't
// be needed anymore.

or

// XXX (bug): drunk, fix later

It’s easy, flexible, and greppable. If you have a lot of code taking care of backwards compatibility, you’ll want to spend extra attention on making it easy to remove when it becomes obsolete.

Sometimes, grepping won’t help and you have to work harder. To cut out the bad part, you need to shuffle things around, refactor, and build some new pieces. But the feeling of making it better is totally worth the trouble.

Don’t be afraid of deleting obsolete code. If you ever need it back (and I’ll bet you won’t), it’s going to be right there in your version control history, waiting for you to resurrect it. Don’t #if 0 or comment it out. Remove it. The best code is no code at all. The code is our enemy. Don’t treat the code as an asset. Treat it as a liability.

So… What is going to be your satisfying commit of the day?





Related posts:

  1. Get Rid of Source Code Templates
  2. Programming Problems in Disguise

If you liked this, click here to receive new posts in a reader.
You should also follow me on Twitter here.

Comments on this entry are closed.

{ 5 comments }

Giorgio Sironi September 14, 2009 at 15:11

I like the economic metaphor, if you think about it code is really a liability, a thing which subtracts money (time) from your pocket. Maintenance time and debugging time are precious resources.

Ville Laurikari September 14, 2009 at 21:23

Yep, I always smile, inwardly at least, when someone announces that “the code is the crown jewels of this company!”. Um, what about the people who created the code? If the code is so precious, then the developers must be geese laying golden eggs!

Susanna Kaukinen March 21, 2010 at 22:20

When I’m writing a program, I first come up with more and more code. When the program begins to become complete, I find myself removing code.

Ville Laurikari March 22, 2010 at 14:05

I find myself doing the same thing. I think it’s because programming is really a process of discovery. Usually you don’t know beforehand exactly what code you need to write, even if it was crystal clear what the code needs to do. So as you explore the space of programs that solve your problem at hand, you end up programming yourself into several branches which become obsolete when you discover better ways to solve the problem. When finishing up, you prune all those obsolete branches and leave in the best solution you could come up with.

Susanna Kaukinen March 23, 2010 at 02:14

Very well put! :-)

Previous post:

Next post: