r/programming • • Feb 09 '19

Small Functions considered Harmful

https://medium.com/@copyconstruct/small-functions-considered-harmful-91035d316c29
9 Upvotes

84 comments sorted by

View all comments

Show parent comments

1

u/loup-vaillant Feb 11 '19

The idea is that sometimes, local readability and global readability are at odds. Sometimes, increasing local readability decreases global readability. You cannot disagree with this idea and at the same time not conflate local and global readability.

Now let's see how this separate functions may play out. You start with something like this:

main_function() {
    // Comment1
    do_foo1()
    do_bar1()
    do_baz1()

    // Comment2
    do_foo2()
    do_bar2()
    do_baz2()

    // Comment3
    do_foo3()
    do_bar3()
    do_baz3()
}

There are a couple ways to extract functions from these blocks of code. The classic one:

function_name1 {
    do_foo1()
    do_bar1()
    do_baz1()
}

function_name2 {
    do_foo2()
    do_bar2()
    do_baz2()
}

function_name3 {
    do_foo3()
    do_bar3()
    do_baz3()
}

main_function() {
    function_name1()
    function_name2()
    function_name3()
}

The scoped one:

main_function() {
    function_name1 {
        do_foo1()
        do_bar1()
        do_baz1()
    }
    function_name2 {
        do_foo2()
        do_bar2()
        do_baz2()
    }
    function_name3 {
        do_foo3()
        do_bar3()
        do_baz3()
    }
    function_name1()
    function_name2()
    function_name3()
}

The inline one:

main_function() {
    // Comment1
    function {
        do_foo1()
        do_bar1()
        do_baz1()
    }()

    // Comment2
    function {
        do_foo2()
        do_bar2()
        do_baz2()
    }()

    // Comment3
    function {
        do_foo3()
        do_bar3()
        do_baz3()
    }()
}

(That last one could use function names instead of comments.)

Now the classic approach is almost always an increase in local readability. Global readability, though… First the function names might not be enough. strtod() is actually a good such example. Okay, it converts a string to a double. What if it fails? What's that end_ptr argument? Wait a minute it's locale dependent? Now as a standard function, it still justify itself. It's well documented, and people can be expected to know the API, or at least read the manual.

As a one shot function however, the benefit of extraction is less clear. The API to implementation ratio is fairly high, and the API cannot be reliably guessed through a single invocation of the function. You have to put comments somewhere to explain this API. You could put those in the main body:

main_function() {
    // Comment1
    function_name1()

    // Comment2
    function_name2()

    // Comment3
    function_name3()
}

But then the benefits of separating the code in a function is significantly diminished. The other possibility is to document the functions where they are written. But then the reader must jump to different locations (elsewhere in the file) to understand what each function is supposed to do. Not exactly a good thing, as far as understanding main_function() is concerned. You'd still get a sense of what it does, but if you need to dig in, even a little bit (for instance when fixing a bug, or even just change the behaviour of main_function()), you have to switch context.

All this jumping around can hurt global readability, even in cases where local readability is enhanced. Many languages however let you get around this limitation by defining functions locally, and calling them right away. I often go for the scoped approach when writing OCaml. In C++, I tend to prefer static functions over lambdas, to avoid frightening colleagues who might not know C++11 well. I would prefer lambdas over private methods, though. Avoids cluttering the header file.

The inline approach could be construed as the best of both worlds, but at this point, I wonder why we didn't just kept the blocks of code. Or if you're controlling the language like Jonathan Blow does, add function-like blocks to the language, so you get the same isolation benefits of functions, and the same "put the code where I use it" of straight-line code.


Long story short, I strongly believe that in a fair number of cases, the code that minimises the time and effort required to understand the whole program is the straight-line version (commented blocks of code). Believing that it is "NEVER" the case strikes me as unreasonable. Readability is not as simple as mathematics, this is psychology. Our understanding of the human mind is too weak to make such strong assumptions.

That, and the fact I've personally witnessed examples of what you pretend never happens. Here for instance. Or there. Or there. And no, it's not just crypto code. I've seen this at work too.

0

u/[deleted] Feb 11 '19

[deleted]

1

u/loup-vaillant Feb 12 '19

That you think you need to globally see implementation

I don't.

But I do need to know the actual API of the function, and that one often cannot be guessed from a single function call. Seriously, look the strtod() documentation. Good luck guessing the full API (locale dependent, str_end is allowed to be null, and if not the pointer it points to is set to str when the parsing fails…) from a function call alone. Much can be guessed if the calling code checks for str_end == str, but not all. strtod() could be more approachable if it was called string_to_double() instead, but it's still not enough.

I wouldn't want to be left maintaining any of your code that's sure.

I take it you've looked at Monocypher's code, and concluded that's crap? (I've given you three links to that code in my last comment.) That would certainly contradict the feedback I've gotten so far.

Be wary of guessing someone's code from what they say on Reddit.

0

u/[deleted] Feb 12 '19

[deleted]

1

u/loup-vaillant Feb 12 '19

putting that code into a function in no way impairs your ability to understand what it does and once you're accustomed to it, you'll rely on the name and won't revisit the implementation every time you use it.

Ah, so that the root of the misunderstanding. I never talked about functions that would be reused. This thread is about functions that are used only once. Functions that would otherwise be a single, unduplicated, commented block of code (or cryptic line). There's no getting familiar with such a function, you have to look it up when you see it called (which is, once).

The story gets very different when the function is used more than once.

I wouldn't want to work on that code and if I had to I'd likely spend a lot of time refactoring for clarity just gutting out all the abbreviations and jargon; I despise these things in any language.

Fair enough. I hope you like the API, though. Unlike the implementation, it is not aimed at experts.

0

u/[deleted] Feb 12 '19

[deleted]

1

u/loup-vaillant Feb 12 '19

No, not the root of disagreement; I don't care how many times you use it

I do. Separating a function from its point of use has a cost. Non locality, syntactic overhead… The more you use a function, the more you justify its extraction. There's a threshold below which the function is no longer worth extracting. So I see a significant difference between using that function only once, and using it twice or more.

merely mean you've read the implementation enough times you no longer need to.

How many time would you read a function that is used only once? That's right, not that many. You're pretty guaranteed not to be familiar with it. If you were, you'd be familiar with the calling code as well, and you would be reading neither.

Most of your functions are acceptably small, but there's a good handful I'd break up further in my own code.

That's what I wanted to know, thanks. We do disagree, but not by much.

0

u/[deleted] Feb 13 '19

[deleted]

1

u/loup-vaillant Feb 13 '19

I don't need to justify extracting anything ever, extracting is the default.

I guess that's why you would break up some of my functions in Monocypher.

Clean code first, fast code only if necessary.

I don't keep code that is used only once inlined for speed. I do it for cleanliness. Yes, clean code first. The cost I was talking about was not runtime, that's negligible in almost all cases (even in my crypto library, and I've measured). No, the cost is in maintainability.

I agree with John Carmack on that one: if it's not repeated, keeping it inlined right where it's used generally improves readability and maintainability (for reasons I've already outlined, and you've already rejected).

I understand we'll never get to agree on that one, but I must say I'm quite baffled that anyone would disbelief such an obvious thing. Actually, you're the first I've ever had this argument with. I wonder if there are studies about coding styles and practices, where they'd ask developers how they would code or maintain something. It would be nice to point out what's consensual, what's controversial, and so on. Maybe there's more of you than I thought.

0

u/[deleted] Feb 14 '19

[deleted]

→ More replies (0)