by danuker · 4y ago · 0 comments
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.
Function extraction adds indirection that doesn’t necessarily pay its own rent. It’s not free.
> but at least it should isolate chunks that can be understood with little effort.
The chunks are easier to understand. But the laundry list function that calls them isn't because it's logic is now spread over 10 different functions.
That's... the point of abstractions and programming languages. At work you don't regularly care how System.out.println() is implemented, do you?
If we follow your way of thinking through all we would have would be binary. Maybe assembly.
The whole point of having programming languages and functions is having high level descriptions of what it is doing without having to worry about the implementation unless we have to.
Now, maybe the 1000 lines of code is actually ultra specific and has no bearing at all on everything else, but that's not really common. Could also be that the 1000 lines are more data structure than code.
There is a level of abstraction that's conducive to understanding stuff. Going at higher level costs obscuring implementation details, going at lower level costs making it hard to understand what it does.
One thousand lines of code is probably way too low level. Chunking those 1000 lines into smaller abstractions is already what your brain will do when trying to make sense of it (because the cache is small for abstractions and concepts that aren't already internalized)