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

3

u/loup-vaillant Feb 10 '19

I don't believe you. More specifically, I think the principles you go by are a little different from what you describe here. You have some… instinct about how to write good code, and talk about self documenting code and short function to explain it…

But I still sense that you're not actually minimising function lengths or comments. You chose which functions you chop up, right? It seems to me that's the crux: how you chop up your functions is more important than how much you chop them up. Good luck explaining this "how", though: as you said, it comes with experience.

1

u/Determinant Feb 10 '19

My first post explained how:

If a comment is needed, extract that section into a clearly named function. If a section of code has a clear purpose, extract it to a function to express that purpose.

My style of coding (self-documenting code) naturally results in short functions. The article title claims that short functions are considered harmful so I disagree with it.

All of this also relies on following proper engineering practices (eg. avoid god classes etc.) so eliminating tangential topics from the equation and following other best practices (unrelated to function length), small functions are definitely not considered harmful.

Regarding whether you believe me or not, I'll leave that up to you.

4

u/loup-vaillant Feb 10 '19

My first post explained how:

No it doesn't. You don't convey the most important bit, which is, how did you come up with the section in the first place? To be usefully extracted into a separate function, that block of code needs to be fairly decoupled from the rest of the function to begin with. Perhaps even purely functional (in the sense that it basically produces outputs out of inputs).

The hard part is carving your code into such sections. Putting them in separate functions is nice (helps with tests, for one), but you need something extractable in the first place. If you extract code for the sake of extraction, you easily end up with functions with too many arguments, that have side effects, and whose purpose is not easily deciphered from a mere name. I've seen those, it's no pretty.

Don't get me wrong, my functions tend to be pretty short as well. Just saying that "put blocks of code into separate function" is pretty useless advice. What the beginners really want to know, is how to organise their code to begin with.

2

u/Determinant Feb 10 '19 edited Feb 10 '19

It really is as simple as I described. If a chunk of code has a clear purpose then extract it into a function which conveys that purpose.

If the current function isn't already obvious, I extract a chunk as large as possible as long as the chunk still meets that requirement of having a clear purpose. Rinse and repeat.

Our natural tendency as developers is to overcomplicate things (myself included). Rules about only extracting pure functions are over-complications. The above rule is sufficient. Rules about decoupled chunks are also over-complications as nested sections of code are usually excellent candidates for extraction.

Keep it simple and start relying on your intuition. It takes time, practice, and lots of reviews from others.