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.