The Clean Code Blog

by Robert C. Martin (Uncle Bob)

Code Hoarders

03 April 2014

Have you ever watched an episode of Hoarders, or a documentary about a hoarder? Shows like this were popular a few years back. They showcased the tragic, and all-too-common, phenomenon of people who have lost control over their living space by filling it with so much junk that there’s no place for them to live.

These people fill their homes so full of stuff that all they have left are dark and narrow passageways winding through floor to ceiling towers of junk, trash, old food, pet droppings, dirty laundry, and other less mentionable things. The volume of each room is taken up by junk. The counters, the tables, and the furniture are invisible, buried, beneath junk. The bedrooms and bathrooms are piled high. There is no room to move. No room to eat. No room to sleep. No room to live. There have been cases in which dead bodies have been found beneath the piles of rubbish and detritus.

And yet, every day, these pitiful hoarders bring another new load of junk into their homes. And they pile it on top of the towers of older junk. Sometimes those towers wiggle and wobble, and then fall – blocking the narrow paths that have been carved through the mountains of trash.

Google for “Hoarders” if you’ve never seen one of these shows; and be prepared to be disgusted.

If you have watched a show like this, then perhaps you have wondered how anyone could live like that – how anyone could choose to live like that. But think about it. It’s not that hard to understand. It’s easy to rationalize that the “stuff” you are bringing into your house is adding value to your life. After all, it’s new stuff. New clothes. New appliances. New furniture. New books. It’s property! And property has value. So it’s easy to think: “The more stuff, the better.”

It’s also easy to believe that acquiring new stuff is more important than organizing old stuff. After all, organizing old stuff does not add value. It’s acquiring new stuff that counts. So if there’s any spare time, or spare effort, or spare energy, it should be directed towards acquiring new stuff. Organizing the old stuff is a waste.

Finally, once the mess starts to grow, it’s easy to rationalize away any hope that it can be cleaned. You quickly conclude that there’s no point in cleaning anything because a day’s effort, a week’s effort, even a month’s effort wouldn’t make a dent. To quote Dr. Seuss from The Cat in the Hat: “…this mess is so big, And so deep and so tall, We cannot pick it up. There is no way at all!”

###Code

Now of course you realize that I’m talking about code. I’m suggesting that the mentality that creates crufty tangled systems is the mentality of a hoarder. I’m also suggesting that the result is the same: an unhealthy, unworkable, and unlivable environment. But let’s walk step by step through the reasoning.

Have you ever been a Code Hoarder? Have you ever worked on a project that was built by Code Hoarders? Do you wander through dark passageways of floor to ceiling cruft as you attempt to add some new feature or fix some old bug? Do the towers of junk wiggle and wobble and threaten to fall and block your path? Are there whole rooms of code that you dare not visit? Is the structure of the system invisible and buried under piles of new features and dead code?

Do you believe that adding value means adding new features. Do you believe that acquiring new features is more important than organizing old features? Do you believe that effort applied to cleaning up the old system is wasted effort? Have you given up on any effort of cleaning because you know that you don’t have the time to make a dent in the mess? If you have, you are a Code Hoarder; and your work life is a deep and overwhelming mess.

We have a name for the result. We call it Legacy Code. The very term fills us with disgust and despair.

###Legacies

Who gets to clean up that horrific mess when the Hoarders die? Who hires the garbage trucks to haul away the hundreds of tons of junk? Who hires the HAZMAT team to disfinfect the home? Who hires the cleanup crew to scrub and wash, and paint, and repair the home? The children, of course. Or if not the Children, then the Community. The legacy that a Hoarder leaves is a legacy of junk, trash, filth, and the huge effort required to get it all cleaned up. The legacy of a Hoarder gets worse and worse so long as the Hoarder is alive.

But legacies don’t have to be that way. Indeed, most people strive to leave a legacy of improvement. Most people want to leave something behind that makes life better for those who follow. Indeed, it is the work of most people’s lives to continuously improve their legacy.

A true legacy gets better with age.

###Better with Age?

And isn’t that what humans do? Don’t humans make things better with time? If you’ve got an old car in the garage that you are refurbishing, don’t you expect that car to get better and better and better with each passing day? If you are painting a picture, don’t you expect that picture to get better and better with age?

Can you imagine hiring an artist, a mechanic, an engineer, a doctor, a lawyer, or anyone else, whose work products get worse the longer they work on them? How can making things worse, day after day, be the behavior of a professional? How can it be the behavior of a programmer?

A professional team of programmers make their code better each day. The quality of the code improves with time. That’s how you know they are professionals. Professionals make things better with time. The Legacy code left by professionals will be cleaner and cleaner the older it is because it has enjoyed the long attention of those professionals.

Do you make the code better each day? Or does each day leave the code a little bit worse than the day before?

Is your work leaving a legacy of value, or are you just building a monstrous hoard for someone else to clean up?