State your intentions


I recently came across Martin Fowler‘s post about FunctionLength.
In that post he stated something about intention versus implementation. Although it might seem a very trivial thing, the impact on code readability is huge!

For example, if I want to check if a book is written by more than one author, I could to something like this:

That works perfectly well, but:

  • It doesn’t tell me the intention of the code: what did the programmer really wanted to check?
  • It reveals implementation details that I don’t want to know at this abstraction level. I don’t care if the solution is implementing a list size check.

How to refactor this? State your intentions explicitly, by moving implementation detail to the correct abstraction level:

Or even better (but not really relevant for this blog post):

This way, you can stop reading at the abstraction level that contains the expression hasMoreThanOneAuthor, without your mind being cluttered by implementation details. Maybe I will find a better way of checking a book has more than one author without bothering you with it (although in this particular example that seems highly unlikely).

When reviewing code I have now developed the habit to refactor almost every expression that isn’t trivial into its own method. I found that this really improves code readability.

This entry was posted in Coding. Bookmark the permalink.

Leave a Reply

Your email address will not be published.