DEV Community


Discussion on: Why limit function size?

anders profile image

I think your variable example there certainly would qualify as a place where you would benefit from splitting it into multiple functions =)

But it doesn't really relate to the LENGTH of the function, it's more about what it does, the number of variables and concepts that are in the function.

At the core code should do two things:

  1. Work
  2. Be easily understood.

We have functions that are 100:s of lines long, and that is fine because the flow is simple to follow. We do use comments for “sections of code” that could be moved into sub functions, this is to make it more easily parsed by a human.

Some cases where you definitely want sub functions:

  • There is a lot of “local state”, especially if there is very little shared state with the “root” function.
  • There are multi level nested conditions and or loops
  • A non trivial part of the code is repeated multiple times, ex: you need to format a date a certain way, that happens 12 times, sub function that. If it happens twice, and the code to do it is very simple, probably inline it.

Imagine a simple case where we have a function that renders a simple view in a web app, it creates a list of three different types of things, each of these is rendered entirely differently from each of the others, but it is the same view, and the output is very simple. For each collection we have 20 lines of presentation logic. Applying an arbitrary “max 40 lines per function” forces this to be split into 3 functions (or a real weird set of 2 ; ). Forcing the person who modifies the view to handle 4 functions (root and sub functions). And provides no actual benefit compared the “C” style as outlined by Carmack.

But as always, it's always about trade offs. And I don’t think there are any “hard rules”. You and your team need to find what is reasonable for you.

Thread Thread
winstonpuckett profile image
Winston Puckett Author

Thanks for explaining :). It's funny, the other day I was playing with a nav menu in Blazor and ran into that situation.

I definitely agree that "do what makes sense" is more important than an arbitrary rule.