Function Length Debate

Comments debate the trade-offs between short, single-purpose functions and longer monolithic functions, focusing on readability, maintainability, cognitive load, and real-world usability in codebases.

📉 Falling 0.4x Programming Languages
4,521
Comments
20
Years Active
5
Top Authors
#8225
Topic ID

Activity Over Time

2007
5
2008
43
2009
74
2010
80
2011
105
2012
168
2013
153
2014
201
2015
218
2016
384
2017
248
2018
240
2019
391
2020
397
2021
344
2022
323
2023
455
2024
348
2025
328
2026
16

Keywords

e.g YMMV McConnell LOC i.e CoffeeScript functions code function split lines splitting temp def line names

Sample Comments

tazu Apr 4, 2024 View on HN

I'll take a 30k line function that does one thing over 30 1k line functions that are used once...

000ooo000 Mar 17, 2023 View on HN

I'm with you re. short functions. Setting boundaries around code, i.e. scope, and giving that behaviour a well-defined signature (i.e. a descriptive name, what it needs, and what it gives back) makes it easier to reason about the overall functionality. Giant functions reduce my confidence that any given change isn't going to introduce a problem.I like to imagine each execution path through a function as a piece of string. The fewer strings, the fewer kinks, and the less string overa

nilved May 13, 2016 View on HN

Have you considered not writing functions longer than 3 lines?

ummonk Jan 5, 2019 View on HN

If you're trying to split up a 16 line function into multiple functions and having trouble doing it, instead of claiming the original code is bad, maybe you should consider that the original code is good and you just shouldn't be splitting up the function...

rantwasp May 25, 2021 View on HN

4 line functions everywhere is insanity. yes, you should aim for short functions that do one thing, but in the real world readability and maintainability would suffer greatly if you fragment everything down to an arbitrarily small number.

Supermancho Sep 15, 2023 View on HN

Apologies. I wasn't clear. I meant it's a stretch to approve a function that's 600 lines. It would be very hard to reason about. I have clarified in my original post. Thank you for the feedback +1 to you.

josephg Sep 18, 2016 View on HN

I disagree with the use of lots of small 1 line functions, especially when doing algorithmic work.As the article mentioned, the thing you spend most of your time doing is reading. Using lots of small functions means you have to bounce all over your codebase to figure out whats actually going on. In procedural languages a complex 'meaty' function often needs to be understood in one conceptual bite. Needing to bounce all over the codebase to figure out what those little functions are

Roark66 Sep 15, 2023 View on HN

I too find the code on the left/red (linear) more readable. However the version with all the functions is quite extreme. When I'm splitting my code into functions I decide if something should be it's own function on the basis of: is this chunk of functionality required to be reusable? Am I repeating code, only slightly changed?If the answer is yes, a function gets created. I never do what I assume authors did here, find the smallest logical units code can split into and generat

turtle4 Oct 10, 2016 View on HN

It's about eliminating cognitive load when reading and/or modifying source later. The more code you can eliminate from what is being looked at, the easier it is for the reader to understand the purpose of the function and what it is accomplishing.At some point, someone is going to need to change your code. When that happens, it is easier to 'surgically' modify a single method that does one simple thing, than adjust one tiny piece of a much larger function. Even though the

eru Oct 25, 2024 View on HN

> 1. Don't use a bunch of tiny functions. This makes it harder for future eng to read the code because they have to keep jumping around the file(s) in order to understand control flow. It's much better to introduce a variable with a clear name.If you have nested functions, that's not a problem.Btw, why do you use regular expressions for some rules, but not for others? Regular expressions are perfectly capable of expressing the length requirement.