Skip to main content


decomplexifying #curl

daniel.haxx.se/blog/2024/10/27…

#curl
in reply to daniel:// stenberg://

I'm sure you're aware of the caveats but using function length may not be the best metric to measure complexity. What I'm about to write is therefore likely nothing new to you but I'm doing it nevertheless because there are many voices in the software engineering space that apply a naive law of "long function bad, short function good".

While splitting a function into smaller parts may improve readability to some degree, it rarely decreases the mental capacity needed to understand it. I'd argue that it's quite often even the opposite if the code is well-structured:
Blocks can be used efficiently to mimic a function that is specific to a use case, relatively short and not reusable. A separate function would introduce overhead in form of its signature and disruption of the reader's flow because its location is elsewhere in the code. The block can easily be shoved into a function when it becomes larger or when it can be re-used.

in reply to daniel:// stenberg://

As a general rule of thumb, I agree, I think.

When it comes to reducing complexity, what I'd want is to keep mental capacity needed in an acceptable range. In that regard, I'd e.g. value minimising the amount, scope and lifetime of variables within a function over function length. These metrics are likely much harder to provide but maybe they would be a neat addition next to the cyclomatic complexity.

in reply to Lenny

@f09fa681 I would also add making sure symbol names are short, readable and not easily mixed up. Hard to measure with a tool...
in reply to daniel:// stenberg://

@f09fa681 Kinda agree when we exclude functions extracted/refactored that contain only one call. While a complex call with a GOOD name of the function might be considered helpful, I tend to find it harmful as it is a context switch while reading code, for only one line/call. So it can turn bad if everything is wrapped in small functions - having unreadable code, and slipping right through the metric.