How to structure functions

When it comes to writing functions, there are two extremes:
One really big function that does all the things. Lots of really small functions that do just a single thing. In my experience, beginners tend to write really big functions, while more seasoned devs tend to break stuff up into lots of really small “pure” functions.
While I think smaller, more narrowly focused functions are better for readability and maintaining code, it can also lead to code bases that are big nested dolls of functions calling functions that call other functions that call more functions.


This content originally appeared on Go Make Things and was authored by Go Make Things

When it comes to writing functions, there are two extremes:

  1. One really big function that does all the things.
  2. Lots of really small functions that do just a single thing.

In my experience, beginners tend to write really big functions, while more seasoned devs tend to break stuff up into lots of really small “pure” functions.

While I think smaller, more narrowly focused functions are better for readability and maintaining code, it can also lead to code bases that are big nested dolls of functions calling functions that call other functions that call more functions.

That kind of setup is in many ways harder to read and maintain than one really big function (and it’s worse for performance, too).

I use three basic guidelines for when to break parts of a function into separate, smaller functions…

  1. Is it getting hard to read?
  2. Is it hard to name because it does too much stuff?
  3. Does any of the same functionality get used elsewhere?

If the answer to any of those is “yes,” it’s time to split at least some of the function into its own separate thing.

If not, it’s probably ok to keep it where it is.

Like this? A Lean Web Club membership is the best way to support my work and help me create more free content.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2025-01-17T14:30:00+00:00) How to structure functions. Retrieved from https://www.scien.cx/2025/01/17/how-to-structure-functions/

MLA
" » How to structure functions." Go Make Things | Sciencx - Friday January 17, 2025, https://www.scien.cx/2025/01/17/how-to-structure-functions/
HARVARD
Go Make Things | Sciencx Friday January 17, 2025 » How to structure functions., viewed ,<https://www.scien.cx/2025/01/17/how-to-structure-functions/>
VANCOUVER
Go Make Things | Sciencx - » How to structure functions. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/17/how-to-structure-functions/
CHICAGO
" » How to structure functions." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2025/01/17/how-to-structure-functions/
IEEE
" » How to structure functions." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2025/01/17/how-to-structure-functions/. [Accessed: ]
rf:citation
» How to structure functions | Go Make Things | Sciencx | https://www.scien.cx/2025/01/17/how-to-structure-functions/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.