5 ms·
> 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
by d--b 9y ago
> 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.
- jghn 9y agoDefine "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.
- exergy 9y agoWhich 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".
- calafrax 9y agoyes it is actually. if your function has a logical branch then it is doing more than one thing. if your function has a logical branch within a logical branch then it is doing exponentially more things and this complexity continues to grow exponentially. therefore, if you have a logical branch you ask yourself: does it make sense to create a function here? the answer may be yes or no. if you create a nested logical branch you ask yourself: does it make sense to create a function here? the answer still may be yes or no but the weight of the evidence for yes has increased exponentially.
- Veedrac 9y ago> 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.
- jerf 9y agoI've heard rumors of people operating under a "all functions must be one line" rule, though I've never encountered it in real life.
- falcolas 9y agoMy linter throws up a warning if there are more than N lines, M private variables, P branches, or Q return calls (or more than 80 characters in a line. Grr). So, yeah. People have definitely internalized these as the law and no longer question it.