How Long Should Functions Be? How Do We Measure It?

Is there a max number of lines for functions?

Sara Khandaker
The Startup
3 min readAug 17, 2020

--

We are all very familiar with how functions help us write better code. They provide two main benefits, reusability and abstraction. This creates code that is DRY and easy to follow.

Now you might also be familiar with the idea that your functions should be small. The smaller the better! When doing my Bootcamp I had an instructor mention that if a function was more than 5–10 lines you should consider making another function. I did not stick to this rule. So I decided to research this and see how relevant this was.

Side note: I am using the term “function” generally here. They are functions in JavaScript and they would be methods in Ruby and subroutines in Java etc…

Measure: number of lines

I spent some time researching the number of lines a function should be and wow the answers were varied! Some will say more than 5 lines need a refactor, some will say 5–15 lines, some say 30 and some say below 100 is all good. Now that’s quite the range! And with little to no explanation on why a certain number of lines means anything at all. For the most part, it seems people did agree that number of lines is not a good indicator for length.

Now even though you can’t use an exact number of lines of code to determine if your method is too long, if you see that your function is over 200–300 lines it’s probably worth a second look. It can help you identify especially lengthy functions that may benefit from a refactor.

Since the number of lines is not a good indicator of how long your functions should be, here are some other measures that may be helpful:

Measure: the size of your screen

One measure I came across in my research is the size of your screen. Essentially, if your function is larger than what fits on your screen at one time then your function is too long. There should be no scrolling required.

There are some benefits to keeping your functions at this length:

  • It is easier to see the algorithm as a whole when you can see it all together. The flow of the logic is easier to see and easier to hold in your head.
  • It makes it easier to spot a bug or find a specific block in the code. Finding this block of code would be much harder if you had to sift through many lines of code and scroll as you look.

Measure: the number of things they do

A function can also be measured by the number of things it achieves. This is an excellent measure of a function since functions should do one thing. This helps keep your functions from becoming too complex. Some benefits of functions having a singular purpose are:

  • If they do one thing, they can focus on doing the thing well. Refactoring can be more efficient.
  • It helps make your code self explanatory. With descriptive naming, it is clear what each function does and fewer comments are needed.
  • It is possible to perform more accurate unit testing

Measure: getting the job done

Now you can try to follow all the measures above and see that that in some cases they just simply don’t work. And guess what? that’s ok! At the end of the day, the only measure that will matter is if your functions do the tasks they are supposed to.

One major exception to the small function idea is switch cases. Sometimes it’s justifiable to have a large switch statement with many cases that end up being lines and lines of code. However, if the logic is sound, there is no need to make this function smaller.

In short,

Question: How long should your functions be?

Answer: As long as it needs to be but also as short as possible.

References

--

--

Sara Khandaker
The Startup

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio