Live data from Hacker News

Small functions considered harmful

medium.com

1–10 of 121 posts

Re: Small functions considered harmful

#2
I find a lot to agree with here.

It's all very conceptually neat and (if you're lucky) easy to read from the top down, where you enter one function and read off a list of other functions which are called in order.

But then if you look into any of those other functions they also call more functions and so on, several levels deep. And when you have to debug someone else's code because the data after function 15 of 17 isn't quite right, and you have to unpick all the places it's been passed through in slightly different versions and slightly different lists of parameters, it can be a nightmare.

Same with my linter telling me to close a file within a few lines of opening it. Personally I'd rather keep all the file-munging code in one place rather than scatter it down a rabbithole of nested functions as an exciting Alice In Wonderland story for future developers.

I try to come to a compromise on these things when working in a team, though...

Re: Small functions considered harmful

#3
Shortness in a function is correlated with quality in design, but it doesn't cause quality in the design.

When we simply follow formulaic advice (keep all of your functions short) we lose sight of the wisdom behind why this was wanted in the first place.

The goal is to develop the wisdom, that makes you a great engineer, not to "follow all of the rules"

Re: Small functions considered harmful

#5
> The idea that functions should be small is something that is almost considered too sacrosanct to call into question

Errr... Really?! I thought we all agreed that the first rule of programming style is that "it depends"...

When they say "small functions", they mean "not the 5000 loc VBA macro that has 50 Boolean arguments, and 30 side effects".

Breaking a function that does 1 thing into sub functions just so that each of them is smaller is not a good thing. And I think people realize that fairly quickly.

Re: Small functions considered harmful

#6
post #5

> The idea that functions should be small is something that is almost considered too sacrosanct to call into question Errr... Really?! I thought we all agreed that the first rule of programming style is that "it depends"... When they say "small functions", they mean "not the 5000 loc VBA macro that has 50 Boolean arguments, and 30 side effects". Breaking a function that does 1 thing into sub functions just so that ea…

Define "a thing". This is the crux of all of these debates, IMO. Most people agree a function should do one thing, people tend to disagree on the granularity of things.

Re: Small functions considered harmful

#7
post #6
post #5

> The idea that functions should be small is something that is almost considered too sacrosanct to call into question Errr... Really?! I thought we all agreed that the first rule of programming style is that "it depends"... When they say "small functions", they mean "not the 5000 loc VBA macro that has 50 Boolean arguments, and 30 side effects". Breaking a function that does 1 thing into sub functions just so that ea…

Define "a thing". This is the crux of all of these debates, IMO. Most people agree a function should do one thing, people tend to disagree on the granularity of things.

Which is precisely why debates like these can occur in the first place, it's not an exact science. Which is also why the parent started with "It depends".

Re: Small functions considered harmful

#8
post #5

> The idea that functions should be small is something that is almost considered too sacrosanct to call into question Errr... Really?! I thought we all agreed that the first rule of programming style is that "it depends"... When they say "small functions", they mean "not the 5000 loc VBA macro that has 50 Boolean arguments, and 30 side effects". Breaking a function that does 1 thing into sub functions just so that ea…

> When they say "small functions", they mean "not the 5000 loc VBA macro that has 50 Boolean arguments, and 30 side effects".

Clearly, though, not all of them do. From the article, for context.

> The first rule of functions is that they should be small. The second rule of functions is that they should be smaller than that.

> In my Ruby code, half of my methods are just one or two lines long.

> Any function more than half-a-dozen lines of code starts to smell to me, and it’s not unusual for me to have functions that are a single line of code

> I’ve worked on codebases inherited from folks who’d internalized this idea to such an unholy extent that the end result was pretty hellish and entirely antithetical to all the good intentions the road to it was paved with.

It seems to me there are a lot of people who espouse making your functions extremely short, and for who a 30 line function counts as long.

Re: Small functions considered harmful

#10
post #3

Shortness in a function is correlated with quality in design, but it doesn't cause quality in the design. When we simply follow formulaic advice (keep all of your functions short) we lose sight of the wisdom behind why this was wanted in the first place. The goal is to develop the wisdom, that makes you a great engineer, not to "follow all of the rules"

> The goal is to develop the wisdom, that makes you a great engineer, not to "follow all of the rules"

This is why books like Clean Code can be harmful. It can be extremely dogmatic if blindly followed, which is very common unfortunately.

Post reply on HN