Reuse is overrated

I read today somewhere in the session notes from XP Day London 2008

Rails ControllerViews discourage re-use all the way up to the UI.

I disagree. It reflects a point of view that is wrong in a fundamental way. Rails controllers tend to be very simple. The code in the controller that responds to an action (a user clicking on a button, for instance) is usually very short, from 1 to 6 lines of code. The code being so short, there is little value in reusing it; it’s much faster to just rewrite it. Not copy-and-paste it, mind you: just rewrite it. There are plenty of ways in Rails to factor out code that is common to more methods, when you want. But the real joy of Rails is that writing the code that responds to an action is so simple, that it usually takes less than a minute to write it from scratch. So it’s true that in a well-written Rails application many controller methods will look quite similar. It’s OK, because readability is more important than refactoring every microscopic bit of duplication.

So why the concern about reuse? Well, it comes out of the idea that writing software is difficult. So when I finally get some piece of software really working, I hang on to it. It’s precious. It was so difficult to write that I’m afraid it would take at least as much time to rewrite it. And I’m afraid I would not even be able to write again correctly. And more often than not, it’s so tangled and unreadable that I’m not sure I even understand it.

It’s not wise to let fear be the main driver of our actions. Let’s revert this. Let’s start throwing away code! The second and third time I write something, it will come out different and better. Sure, writing software is difficult; like playing the piano is difficult, like handwriting, like riding a bike, like baking pizza. They are all difficult activities, and they all get better with practice. With good, proper practice.

Let’s practice rewriting code until it becomes so easy and so quick that the old concerns about “reuse” will become a quaint thing of the past!

3 Responses to “Reuse is overrated”

  1. Paolo "Nusco" Perrotta Says:

    Best post ever on this blog. :)

    Heavy cross-project code reuse used to be one of the main selling points of OOP. It just didn’t happen – not in the sense we expected (we did get library reuse and conceptual reuse, that wasn’t really what we were waiting for). Your post explains why. This is quoting fodder!

  2. Stefano Leli Says:

    Compliments for this good consideration on code reusing. You explained in few lines the real essential of this practise.

  3. Thibauld Says:

    Good post!
    I cannot agree more with you! Most people don’t realize how much time they loose when they try to reuse code instead of actually coding the few lines required! By the way, I just blogged about the exact same issue a few days ago.

Leave a Reply