4 ms·
I can agree "code should be in small functions" by itself is a bad guideline. I think it's suggested due to other principles around making code easier to read /
by rgoulter 7y ago
I can agree "code should be in small functions" by itself is a bad guideline.
I think it's suggested due to other principles around making code easier to read / use.
It'd be better to ask if the code is more complex than it needs to be.
I liked the terms John Ousterhout uses. For some module of a program (e.g. a method, class, package, etc.), it has an interface and an implementation. The interface is "what you need to know to use the module", the implementation is "how it's done".
Complexity comes from dependencies (e.g. more interfaces you need to know about), and from obscurity (e.g. things you need to know about, which aren't obvious from the interfaces given).
In some cases, many small methods would be an increase in complexity overall. In some cases, one big method may be an increase in complexity.
- hinkley 7y agoIn most cases one big method is an increase of complexity (and others will add to it over time). Large methods are usually a failure of imagination, like bad and rambling prose. Code is a set of instructions for the computer, and for the next person who has to maintain it (“ Programs are meant to be read by humans and only incidentally for computers to execute.”) Cooking instructions are split into steps. Assembly instructions are split into steps. LEGO instructions are split into steps. When drawing instructions aren’t split into steps the Internet turns them into a cultural phenomenon (Step 2: draw the rest of the fucking owl). Only programmers think they are immune to this ridicule, smash all the steps together, and then get salty when others complain, ignoring a series of luminaries who beg in every format of media available, and for decades, to do otherwise. Seriously, organize your code, separate the steps. You’re killing us.
- mason55 7y agoNo one disagrees that code needs to work in steps. The conversation is around how much and the answer is going to depend on what you're doing. Why have a function that prints a whole line? Isn't that just smashing together a bunch of small steps to print one character at a time?