by wruza · 4y ago · 0 comments
But there is a single “case” I’d never agree on: the code whose complexity and/or composition ends with nothing. The more experience you get, the more high-level structure you are able to pick on, but sometimes structures are just there, serving nothing at all. Overzealous (or compelled) decomposition into 5-line chunks over tens of files. Pointless renaming, re-exporting and encapsulation. Higher-order code golf. All this done to a finished non-extensible project makes no sense and makes it harder to read, for no reason.
This is a lenghty article, but it is readable in one chunk, a good piece of read. Now imagine the author split it into a number of submodules, then in each they’d give a new name to every phenomenon. Then instead of using English they’d construct a new sub-language (also in modules) to express meaning in a shorter way. E.g. this:
I can’t read the code because I don’t have sufficient experience or expertise (with the language or domain).
I haven’t spent enough time trying to read and understand the code (“it’s not obvious” or “it’s not intuitive”).
I don’t have much interest in understanding this code, I prefer to rewrite it in my own style.
…
turns into this: With code as object, let me as you negating:
frobnicate causality of read object over sufficient experience or expertise (with the language or domain).
frobnicate relation of enough time and attempt to read and understand object (“it’s obvious” or “it’s intuitive”).
have much interest in understanding object, no prefer to rewrite it in my own style. // “no” negates negation
include more items from [here|https://…].
See frobnicate in my frobnication article.A 1000 line function broken down into 100 different 10 line functions which are then each called in turn (and need lots of parameters to pass around the inevitable shared state) is actually often less readable than just a 1000 line function.
The way to deal with such a function to ask why there needed to be a 1000 line function. Why does so much apparently need to be done here and now and so much state shared across so many lines.
Code metrics tools would rather you break everything down into micro-functions which if you're blindly trying to follow the tooling recommendations to get it past a check-in gate will probably end up badly named and with little thinking into actual abstraction.
If you have two functions but whenever you call function foo = Bar() you have to then call function Baz(foo) then you don't really have two functions at all.
This would help testing and understanding. You can test and understand the parts independently, and only have to test the huge function in order to check that the parts are attached correctly.
https://rhodesmill.org/brandon/talks/#hoist
https://danuker.go.ro/the-grand-unified-theory-of-software-a...
But now imagine that it returns the complete text of Shakespeare if the input it 'bob' (why? customer requirement. their business literally falls apart if this doesn't happen). In this case you probably shouldn't pull it out into it's own thing because it's so specific (and weird) that you'll want it nearby it's use case (and not available for developers to accidentally use for the non Shakespeare uses).
* yeah this is convoluted. There are better ways to factor such a function. However the point is that something can be 'pure' but still not a good candidate to be pulled out of context.
I guess it depends on how much this weird Shakespearean to_lower is used around the codebase.
If there is one spot where it's used, I would probably factor out to_lower and add a special case check for "bob" in the business logic code (outside the scope of to_lower).
If the Shakespearean to_lower gets used in various places it actually might make sense to be factored into its own function.
Do these bits of pure functions appear anywhere else in the code? If so, sure, I happily pull them out into their own functions.
If not, I will leave them exactly where they are. It's easier to read a laundry list than to look up many different parts in isolation without context.
As for testability: If some bit of code only appears in that one long laundry-list function, what do I gain by testing it outside of that context?
1000x1 lines of code all at once have a lot of space for hidden non-obvious interactions. Maybe it won't have that, but I haven't ever encountered it.
Making it 10x100 lines of code won't necessarily make it better, but at least it should isolate chunks that can be understood with little effort.
Having to understand 1000 lines of code before being reasonably certain you can make a change is far harder than understanding some 20-50 lines.
Someone new started and... within 10 minutes commented in the code "150 lines is way too long, we need to understand why this is so bad and how to refactor this to be good - you should never do so much in a method".
Could I break it up even more? And have one method that composes 4 then each compose 4 that each compose 3? Possibly. But there's a tradeoff, and... what annoyed me more was that there was 1) no questions asked, 2) no view of the history of how it went from 800 down to 150, 3) no review of the tests in place which document and demonstrate that it does, in fact, work.
Pursuing brevity at all costs has a higher price than some people understand. Worked with the person a while longer, and they eschewed tests because... "hey, everything I write is already so small and understandable, there's no need for tests", demonstrating a pretty basic misunderstanding of tests (imo).
I have seen 1500 method in an app I was rewriting. The dev no longer worked for us. It was badly written, I told the management it wasn't optimal and there were some issues with it but I said I don't know under what constraints the dev was working. I simply wasn't there when it was written and have no idea if it was incompetence or some other factors.
The point is anyone who thinks certain way is BAD and needs to be immediately improved while barely knowing the code base is a bit incompetent. I honestly would wait weeks to start bringing up some issues I may see in the code base or sql. I would learn the team dynamics first and try to see if there is any other reason for those things.
let (x, y) = {
... (some code that only serves to create x and y)
};
I really really like that feature for controlling scoping - it's like a single use function that doesn't involve so much navigation.Other languages allow similar constructs with blocks too. For ones that allow inline lambdas, you can also get similar results by defining and immediately calling the function.
Aside from the 1000 line function (sheesh), this is a failure of the IDE, not of the practice. We have computers that can inline these functions so you could see the code in a big 1000 line thunk if you would like, but only a LISP IDE gives you inlining afaik.
> “Good code is simple” doesn’t actually say anything.
That's a bold claim and I will assert it is wholly incorrect. Good Code is simple and simple code is VERBOSE
> whether that translates to “simple” code depends on the programmers.
That is incorrect. It largely depends on the language abstractions. When the abstractions are familiar, it goes from incomprehensible (literally) to familiar. Code cannot seem simple if you do not have the familiarity with the abstractions used.
> Should we strive to satisfy the Shakespeare for Dummies demographic
Yes.
> Programmers seem to believe in a realm of beautiful, readable, easy-to-maintain code that they haven’t seen or worked with yet
Lots of programmers believe it because they have seen it. I have. It was embedded in hard-to-maintain code, but it was there.
It doesn't mean anything because both good and simple are subject to on-the-spot redefinition. At what skill level does code become "simple"? Does all code have to be ELI5 to be good? When a Node developer looks at CUDA implementation of k-means regression analysis and finds it "difficult", does that mean the code isn't good?
When the abstractions are familiar
So you're saying it does depend on the programmer, because the programmer should be familiar with the abstractions used?
Short answer: No. "good" is a different bar (whatever you mean by it). It's not possible for every function to be simple, for more than 1 reason (for sure, some calculations are inherently complex, some relationships are complex, etc). However, the vast majority of code can be.
> So you're saying it does depend on the programmer, because the programmer should be familiar with the abstractions used?
Correct. It is expected the viewer is familiar with the language, not additional abstraction on top of that. You have to assume there is a cost to those new abstractions that elevate it beyond simple.
This can be argued the other way too. Decent IDEs have code folding and other features so you can look at a long function at a higher level without needing to abuse language features for subjective purposes.
Function inlining is folded, moreso...
foo() { return bar(); // <-- bar can be clicked on and expanded in place }
becomes:
foo() { return {return "boo";} }
------
bar() {return "boo";}
Exactly.
For decades, people were told this is the way to go, and would somehow, almost magically, make code readable, maintainable, reusable. The result is the exact opposite, a gigantic pile of needless abstractions and spread-out functionality, most of which exist solely to satisfy some paradigms.