Aside from the clickbait-y title, I find it quite disturbing to base that whole piece on, what even the author describes as, problems in "codebases inherited from folks who’d internalized this idea to such an unholy extent". Indeed, small functions can be bad if you completely and utterly overdo it. But wait, that's true of nearly everything else.
Small functions considered harmful
31–40 of 121 posts
Re: Small functions considered harmful
#32So, the author thinks this is harmfull?: var area= ( width, height ) => width * height; If not, it is just clickbait for me.
It would be harmful if you were calculating something subject to change, like, say price.
price = (cost) => cost * 1.10;
And you sprinkled that throughout your code. The problem arises when the business says they want to be able to change the logic so that it lowers the price for customers that have subscribed over a year. Now you have to change the logic everywhere that shows up, rather than having a single function where you change it once: function decimal Price(cost) { .. }
That in essence, is why DRY is important: code maintenance & refactoring.Even your example it could be dangerous:
var area= ( width, height ) => width * height;
What if the business was making sandboxes and they wanted to be able to add a short wall around it so cats have a harder time using it as a bathroom. It would then be: var area= ( width, height ) => (width + 1.0) * (height + 1.0);
Again, if you had sprinkled that throughout your code, you would have to search and find every instance of that and change it. This is also harder to test, because if you miss one, chances are, it won't show up immediately.Re: Small functions considered harmful
#33Obsessive decomposition of the sort Fowler, for example, is cited preferring, very quickly becomes pathological - I suspect that the codebase Fowler describes in that tweet, to the developer as yet unfamiliar with it, reads like one of those old IBM field engineer manuals where a giant circuit diagram is spread across 800 letter-size pages, all bordered with dozens of numbered arrows each referencing the page on whic…
But if the stuff that needs to change is abstracted, it only has to change in one place. IMO, the point of "prefer duplication over the wrong abstraction" is that "all the stuff that needs to change" is a partially-to-totally unknown quantity. You can either guess at the time you have the minimum possible amount of knowledge (when the code is initially written) or wait and make that decision later with more evidence.…
Re: Small functions considered harmful
#34 A()
B()
C()
Called in sequence is that they smell of imperative code. A recipe of some kind, of steps performed in sequence. That kind of code, when it occurs, should probably just be performed in a single function. That is - until either A, B or C can be re-used by other code without creating an unnatural abstraction. If the steps as separate functions can be tested separately - great. But if they are only ever used in this sequence - do you really need to test them individually? What good does the breaking up into functions do, compared to just some comment text in a longer method. // Step A:
... 3 lines
// Step B:
... 4 lines
// Step C:
... 3 lines
Answer: very little. And it forces the reader to scroll to see the relevant code. Some languages allow the use of local functions - which is basically just some trickery to help with variable scope and not have to use a comment line to call the sub-step something. Can be quite useful.A better example of 3 functions is when you have
y = A(B(x))
and then turn it into y = A(B(C(x)))
If the C can have some kind of semantic meaning (e.g. C just fetches the price of item x before the rebate is applied by B). In this kind of functional code there is usually very little harm in making more and smaller functions. Not sure where I'm getting with this but I assume It's kind of an argument for avoiding procedural code to begin with, and aiming to make actual functions.Re: Small functions considered harmful
#35Re: Small functions considered harmful
#36Re: Small functions considered harmful
#37Seriously, HN should have code that auto flags anything including a subject line of "considered harmful".
Re: Small functions considered harmful
#38where X is any number of lines necessary for implementing the ONE thing that function should be doing
Any attempt to replace X with a concrete number will invariably sacrifice simplicity for the sake of that number.
Re: Small functions considered harmful
#39Earlier quoted context omitted.
Clean Code can be harmful? Or the person dogmatically applying the advice? Martin warns against such blind dogmatism. He even goes so far as to explain the thought process behind most of his recommendations so that one may consider whether the rule applies or not. On balance, Clean Code does far more good than it does bad. I have yet to see these dogmatic Clean Coders blindly applying Martin's advice. Not in industry…
> I have yet to see these dogmatic Clean Coders blindly applying Martin's advice. You're lucky. I've seen it plenty of time. In one extreme case we even had to let go a perfectly (formerly) competent dev after he read that book and went crazy (yeah, seriously).
Doubly so if it was on Coding Horror.
Re: Small functions considered harmful
#40> an awful lot of times, pragmatism and reason is sacrificed at the altar of a dogmatic subscription to DRY, especially by programmers of the Rails persuasion.
I'm not making this a tribal thing but it does sometimes feel like there's a cultural tendency in the Ruby and Javascript communities to... well... preach a little bit. All advice is flawed and most maxims are only partially true.
If a community bounces from one "one true way" to another all the time then it's probably not a particularly healthy environment for those who are learning - as they tend to lack the experience to put advice into the correct context.