Nagging at complete code

Posted on Mon 20 March 2017 in Books

Background

In French universities, we are taught a perfect function is the one which has 25 lines of code which do not exceed 80 characters length. Like a brainless Pavlov dog, I have been, for a while, obsessed by this thumb of rule: if the number of lines of my function is less than 25 then I had to invent additional lines to please my teachers in the hope to get higher marks in my development projects. The result was that I was trying to reach that gold number and missed resolving the actual problem. Later I met developers, some of whom graduated from well reputed schools in Paris trying to convince me of the virtues of this rule. So how long should a good function be?

The verse

"Complete Code" is one of the best practical guides to programming that developers cherish. I owe a lot to this book. However, there are few statements I do not agree with. Namely when the author wrote on Chapter 7, page 174 this:

"The routine should be allowed to grow organically up to 100-200 lines, decades of evidence say that routines of such length no more error prone then shorter routines."

Criticism

I would like to shake sadly my head on Steve McConnell for the following reasons:

  • Poor design: Long functions reflect a very poor design.

  • Code duplication: Even small functions are prone to code duplication (example), so what could I say about a 100-200 lines function?

  • Scads of unit tests: The longer a functions is the more unit tests a developer needs to design to cover all the possible scenarios. Being forced to write more unit tests than necessary per day while the boss or the client waits for a prototype is not a very good idea.

  • Does not do one thing: A function must do one thing and only one thing. Do not try to convince me that 200 lines of code do one thing.

  • SRP violation: Long functions do not comply to the single responsibility principle. This is a consequence of the the previous bullet item.

Conclusion

Reading the best books written by this or that programming god helps to be a good developer and get benefits from others' experiences. But one must keep a critical eye and refuse to swallow everything that comes from gods.