Reduce, Reuse, Recycle

The Three Rs of Programming

by Rudd Zwolinski

It's a trio of suggestions that you're sure to have heard. Growing up as a kid, I heard this line all the time, from commercials, from school, from my parents.

Reduce, reuse, recycle.

That little triangle with the arrows for corners is burned into my mind for life. Yet, the meaning never really sunk in. To me it all boiled down to the last word: recycle. I learned to separate my normal trash from stuff like bottles, cans, jars, paper, and other things deemed recyclable. To me, that was the epitome of the whole movement.

The first two Rs completely went over my head. I never gave a passing thought to how I might reduce something, or reuse it. When I filled up a paper cup with water, I gulped it down and threw it in the trash can. I can distinctly remember a few times where I would fill a cup, drink it quickly from thirst, throw it out, then go for another cup to fill up, having thrown away the first cup before I realized I was still thirsty. Even though I knew that I should have just kept the first cup, I didn't worry about it. Can't do anything about it now, after all.

I never gave a thought to how I might reuse paper cups more than once, like using only one per day instead of four or five. Likewise, I never thought about reducing my use of paper cups to use, for instance, a water bottle. I could use a water bottle for months, at least. But none of that happened. As far as I was concerned, as long as I was putting my aluminum cans in a separate container, I was doing my part for the environment.

And it's not just me. Plenty of other people missed the message. But recently they've started to get it. I see people carrying water bottles around all day. The recent concern for the environment has sparked a movement that people have really gotten behind. Companies and countries now strive to be sustainable. But what does "sustainability" really mean? On the personal level, there's not much more I can do today to help the environment than there was 20 years ago. I think the main difference is that people are starting to get the other two Rs. Rather than just sorting their waste before they toss it in bins, now people are trying to make less waste in the first place. They're reducing and reusing. They were important all along, but people understand them now.

As it turns out, the same principles make sense in the world of code. Except this time, it's not the recycling we've all perfected.

In the programming world, we're masters of reuse. Laziness is a virtue. We practice and preach "Don't Repeat Yourself", we actively seek out existing solutions, and we always try to build things for others to use. When 37signals extracts Ruby on Rails from one of their web apps and makes it into a full-fledged web framework, that's reuse. When a single developer writes a function for some shared functionality, that's reuse. From our companies' actions to our modular code, programmers live and breathe reuse. It's something that's been drilled into us from our first years as programmers.

However, just like recycling in the world of waste, that's not all you need to do. It gets you a long way, but not far enough.

What about recycling? That is, what can we do to make completely new and better things out of what we have or used to have? On the best end of this, it's like when the PyPy project uses the power of Python to build a Just-in-Time compiler to be able to run a Python program faster than the standard C implementation. On the end that is achievable by programmers everywhere, it's like when you use the knowledge of the many bad design decisions you made the first time around to completely rebuild your product for version 2.0 (or 3.0, or 4.0, etc.). When you're spending days fixing simple bugs or adding simple features because of bad design decisions, or when you're spending hours spending time trying to even find out if you're going to be able to implement a simple feature, then it's time to recycle your code. Armed with the knowledge you have now, you can save yourself days, weeks, months of time by building it right this time.

That's the importance of recycling. However, it has been said that it's almost always a bad idea to do a complete re-write of your codebase:

It's a bit smarmy of me to criticize [Netscape] for waiting so long between releases. They didn't do it on purpose, now, did they?

Well, yes. They did. They did it by making the single worst strategic mistake that any software company can make:

They decided to rewrite the code from scratch.

You'll lose all that knowledge that's stored there in the code, supposedly. Sure, I'm not disagreeing with that. Save recycling for the most desperate of times. Ask yourself if it really needs a rewrite. There are cases where it does. When the Mozilla Foundation built Firefox as a lightweight browser to supplant the heavyweight Mozilla browser, that was a good case of recycling. When Netscape recoded Netscape and ended up with a buggier Netscape, that was a bad case of recycling. In most cases, it's just time to reduce.

Reducing is an exercise best practiced when you're writing the code, not fixing it. Think about whether you're making something a little too reusable. Say you're planning on supporting internationalization. It'll make your text modular, thus making your codebase simpler and easier to maintain, right? Wrong. Trick question! Turns out your code can't be exported out of the country due to proprietary encryption technology. And that's never going to change. So, unless your country suddenly becomes multilingual, you ain't gonna need it. Reduce.

Sometimes you don't have the luxury of getting rid of things early on. Luckily, it's possible when you're trying to clean up your code though, too. Remember that complicated hierarchy you put into the code because you thought you'd be getting clients with more complex needs? Well, it's a year into the project and you've been dealing with headaches from that model for the entire time, and it turned out Fred figured out a way to transform any inputs into the simple format you've been using all along. All that wasted effort for nothing. Don't worry; it was a good thought. But now it's time to simplify. Get rid of that bug-ridden complexity and patch up what you can. Then ta-da! You've successfully reduced. Feel free to relax.

If we embrace the three Rs with our code as we do with the environment, we can reap the same benefits. Reduce, reuse, recycle. It's never too late to take action. •

You should follow me on twitter here. Read more articles here.
Posted on April 07, 2010.