Refactoring

5 Seconds Rule to The Optimal Function Length

Presenting always applicable five seconds rule

Mustafa Katipoğlu
Cyber Architect

--

Photo by Pierre Bamin on Unsplash

When we try to apply the optimum function length recommendations, we struggle to fully comply with them in certain scenarios. Rather than using the known recommendation of 5–20 lines[1], we can use the 5 seconds rule as it can be practiced in every software. The 5 seconds rule can be practiced to achieve high readability in software with boilerplate code.

The Variable Nature of Optimal Function Length

Photo by Jaromír Kavan on Unsplash

The difficulty of finding the optimum function length is mostly caused by the programming languages. As different programming languages try to optimize different concerns, finding an absolute value for the function length becomes hard to achieve.

While the 5–20 lines ranges are applicable in languages that try to optimize developer productivity, this is almost impossible to apply in the languages that try to optimize machine performance. For example, in a C programming function, the declaration part may be as long as 5 to 10 lines even without starting the logic part.

Therefore, in some of the software because of the programming languages or some other architectural constraints, the 5-20 lines range recommendation is hard to achieve.

As I always like to apply the best practices or at the very least come up with an algorithmic way of thinking, here I have come up with my formula of optimum function length.

The Philosophy Behind Optimum Function Length

Photo by Aaron Burden on Unsplash

When we research the optimum function length, we mostly find answers for understanding the general philosophy behind its variable nature.

The ideal number of lines of code in a method is variable. Basically, you only want to write just enough code to do what needs to be done within the context of the function’s definition. I think of this as a kind of Single Responsibility Principle, only applied to a method instead of a class.

Where a method has a lot of logic, and a number of steps to complete, then it makes sense to break the method up into several discrete steps. Each of these steps would be extracted into new methods as required.

“otherwise we would have tonnes of functions, all of which only contains a single line or two of code.”

The less each method does, the more easily defined it is, and the simpler to understand and manage. There is nothing wrong with having hundreds of methods if you need them. Also, in keeping with the SRP I mentioned earlier, it becomes easier to extract new classes when the methods have been teased apart into smaller and more manageable pieces. [2]

5 Seconds Rule

Photo by Jaelynn Castillo on Unsplash

Just like signals use time-domain analysis and frequency domain analysis, here we can also use time as a metric as well as the number of lines.

Here I offer to use 5 seconds rule. The logic part of any function should be understood in under 5 seconds.

There are some of the implications of the 5 seconds rule:

  • The logic part should not be longer than the user view panel.
  • The logic part should be under 20 lines.

When we think about the implications, we can’t think of 5 seconds rule as a brand new one, but just a different perspective of the same rule with a bit more freedom.

When we try to comply with the 5 seconds rule of the optimum function length, we are no longer constrained by the initialization and release part of the code but rather just the core logic part.

The 5 seconds rule pertains to our core aim which is to increase readability with highly readable functions. Therefore, we no longer have the difficulty of finding the optimum function length under different architectural constraints.

Summary

Photo by Patrick Rosenkranz on Unsplash

We can overcome the practical difficulty of applying 5–20 lines of code as the optimum function length by using the 5 seconds rule.

In its core, 5 seconds rule states that only the logic part of the function should be under 20 lines long. The declaration, initialization, release, and other groups of code segments can be safely ignored when counting the number of lines for the function length.

As the aim of small functions is to increase readability, in software with high boilerplate code, the 5-second rule helps us to achieve readability without sacrificing much.

References

[1] Clean Code by Robert Martin, Five Lines of Code By Christian Clausen

[2] Stackoverflow — Is there an optimal number of lines of code per function?- answered Feb 16 ’12 by S.Robins

--

--