Readability is the real (re)usability

Last week on the factor10 DDD course in Cape Town, the question of reusability came up again.  It’s the same old object orientation promise of “Just do OO and you get phenomenal reuse for free”.  Today, I was refactoring some code with another developer at a client and I extracted some lines into a few private methods just to clean up a really fat loop.  The initial reaction from the other developer was “That’s an overkill because you won’t reuse that method”.  My spontaneous reaction was “Readability is the real reusability”.
It’s true, the method won’t be reused.  It’s also true that most us were taught in some Programming 101 course that you should create a method, function, procedure only if you are going to call it more than once, otherwise just leave it all inline.  I value ubuntu coding, and so I have learned to unlearn that naive rule.  When I make my code more readable, I get more reuse out of it.  The reuse I value is not really about the number of repeated method calls or number of inherited classes.  I value the increased reusability that is achieved when more developers are able to read my code and walk away understanding my intention, clearly and unambiguously.

Let me put it another way.  Your code is a representation of your model.  Your model should be used to drive all collaborative discussions about the solution.  That’s where you get the real reuse in your model.  If people can’t understand your model, then your model can’t be re-used for further discussions.

4 thoughts on “Readability is the real (re)usability

  1. > ‘The initial reaction from the other developer was “That’s an overkill because you won’t reuse that method”’
    Did you get what kind of overkill he was thinking of? Performance, of that you were just wasting time?
    > ‘It’s also true that most us were taught in some Programming 101 course that you should create a method, function, procedure only if you are going to call it more than once, otherwise just leave it all inline.’
    I’m often amazed that most devs still think what they learned about optimized code a decade or two ago in school (from a teacher who learned it one or two decades earlier and didn’t keep up) is still applicable. And when you ask them if they heard of HotSpot, they say “No. What’s that?”
    They optimize by inlining code, and then a few lines down, they loop over collection that hits the database once per iteration. Sigh.
    Kind regards
    Niclas

  2. @NiclasThe overkill was about the act of making it another method when it will only be called once, and so perceived performance may well have been the second overkill.
    I like to think that you should read code and then “click through” if you want the details. Just like when we read web pages, right?

  3. When you think of optimisation, the best place is to optimise the most used areas. When it comes to coding you write it once, but you read it many times. Readability is not just for reuse but an optimisation technique too.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s