4 ms·
The readability/understandability points other make are good. But I'd add that large function like this tempt the programmer to do something like using one of
by joe_the_user 8y ago
The readability/understandability points other make are good.
But I'd add that large function like this tempt the programmer to do something like using one of the intermediate variables in the first "bit" to make a decision in the second "bit" out of convenience. Add ten more "bits" and ten more such "conveniences" and you get an incredibly fragile, bug-prone mess.
Which is to say, a purely lexical-order division of operations is bad because you easily wind-up with undefined defined interface between the various operations. Figuring out both how a simple for-loop really works as well as how the original programmer intended it to work, can be quite hard. Add several for-loops and you get a true mess.
Some people get really good at cranking out these long functions and as you say, it look superficially easier. I wonder if a language could support "functional abstraction brackets" that would allow these long sections but guarantee their logic isn't folded together
Something like
function big() {
let x,y,z;
{{@First_bit //begin forced separation...
...
{{@Second_bit //begin forced
...
}
Here "First_bit" would be guaranteed to be equivalent to functions of just x, y, z.
- dasmoth 8y agoIf I'm understanding you right, "C-like-syntax" languages generally allow this: function big() { let x,y,z; { // First bit let localToThisSection = 42; ... } { // Second bit ... } }
- joe_the_user 8y agoYes, If your x,y, and z are both inputs and outputs, that works. If you wanted a syntax that exactly mirrored what happens in the gp's second example, you'd need more work. Plus if you force a begin-bracket to have a label, you avoid the editor having to use "fun3756" when automatically abstracting out the subroutine.
- leppr 8y agoJavaScript's IIFEs can act like that: function big(x, y, z) { const firstBitResult = ((x, y, z) => { ... })(x, y, z) const secondBitResult = ((x, y, z) => { ... })(x, y, z) ... } I've used this trick when hacking out on quick-hack-turns-into-humongous-mess type projects.