decomplexifying #curl
daniel.haxx.se/blog/2024/10/27…
decomplexifying curl
(I wrote about this topic in my weekly email this week. This is the blog version, somewhat extended.) Easy to read Two contributing factors that make code hard to read are function length and function complexity.daniel.haxx.se
Lenny
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.
daniel:// stenberg://
in reply to Lenny • • •Lenny
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.
daniel:// stenberg://
in reply to Lenny • • •Daniel Fisher(lennybacon)
in reply to daniel:// stenberg:// • • •daniel:// stenberg://
in reply to Daniel Fisher(lennybacon) • • •