I think as developers we tend to create complexity if we don’t think carefully about every line of code we are typing. Systems tend to be complex and managing that complexity is one of the main task we have.

Routines are great, they are one of the best ways to encapsulate algorithms and make your code more readable. Sometimes we think routines are too short, but actually the usually are too long. Reading Clean Code I realized Uncle Bob creates very short routines and all of them have a meaningful name.

for (var i=0;i<array.length;i++) {
  show(i);
}

It’s very easy to come up with a name like showAllElements(array). But if you are processing a lot of input parameters (like in a web service) and cleaning the input a good name will be harder to find.

Apart from that with small routines you’ll find your bugs easier and the reuse of that little algorithm is more likable since it’s already encapsulated and ready to be used again.

In the book Code Complete a lot of studies are shown without a clear conclusion but when the author is listing all the valid reasons to create a routine you get the idea that most small routines can achieve that. These are some of them:

  • Reduce complexity

  • Make a section of code readable

  • Improve portability

  • Simplify complicated boolean tests

if ( node <> NULL ) then
  while ( node.next <> NULL ) do
    node = node.next
    leafName = node.name
  end while
else
  leafName = ""
end if

He points out that this code should be encapsulated in a routine like GetLeafName(node).