As your web application matures and your users request more and more features, your code can quickly get out of hand. This leads to hidden bugs, recursive crap and “skeletons” lurking in your code closet.
But we both know it’s important to keep our code lean. But how do you do this when you’re adding features all the time? Or when you’re keeping up with the competition and responding to user requests?
It’s tempting to say, “We’ll just schedule time to refactor on a regular basis.” But as you’re pushing more and more updates, this often gets thrown on the back burner.
Let’s face it: it’s a lot more fun to release new features. It’s exciting. You get to envision something and bring it to life. You hear the “ooohs and ahhhhhs” of users. It feels good to make them happy.
On the other hand, refactoring existing code is like cleaning out the garage. Necessary and rewarding, but it can be dry. And since users can’t see this refactoring work, you don’t get the accolades that you get from new features.
So here is how I keep my code lean as I add more and more features. It’s called “give a toy away”. I use this approach every time I add a new feature to my Rails application or upgrade an existing feature. It’s a regular practice for me and a good habit you can get into. Here’s a little background:
My wife and I live a “lean lifestyle”. Minimal. We value experiences over material objects, so we avoid conspicuous consumption. Our taste in interior design is mid-century modern (think “Dwell” magazine) and even our taste in music includes minimal techno.
Problem is, every birthday and Christmas my kids get new toys. Loads of them. For years, they piled up and became “clutter”. Many were outgrown and obsolete. They took up “mental space”.
Sure, we asked our kids to prune their toy collection from time to time, but it just never happened. So we came up with a new rule. It’s called “give a toy away”.
Give a Toy Away
Here’s how it works: Each time our kids get a new toy or gift, they need to give an old toy to charity.
In other words, they’re forced to make a decision and prioritize what they play with. In with the new, out with the old. The key is that we have them do this the same day. We say, “Look at these great new toys you got! Tonight you can choose a couple of old toys to give to needy kids”. Later that evening, we pull out a bag and have them stuff their old toys inside.
This, their toy collection doesn’t get out of hand and more important, they appreciate what they have. It also makes them feel good that their old toys will be enjoyed by somebody else.
Rails Development “Give a Toy Away”
I apply the same concept to my code. Every time I add a new feature or upgrade an existing feature, I go through my code and find an “old toy” to give away. It could be bloated controller code – like a “find” that is better pushed into a named scope in the model.
Sometimes the old toy is duplication in a view. Maybe I find something I can put into a partial. Or I shove some conditional code into a helper to get old logic out of my view.
Other times I refactor an existing feature completely. Whatever. Every time I look back at old code and see something that can be refactored, I tag it with a…
<%# TODO: Old toy %>
…and give it away when I add a new feature.
My general rule is that I try to simplify as I add complexity. This serves the twin benefits of keeping my app up-to-date and streamlined…and keeping me in touch with old code.
Try it. Get into the habit of giving away an “old toy” for every “new toy” you add. You can’t always make it a line-by-line exchange, but it’s good practice and keeps your code clean.