7 ms·
// procedural style func foo01(a) { b = x(a); c = y(b); r = z(b,c); return r; } // functional style func foo02(a
by tabtab 5y ago
// procedural style
func foo01(a) {
b = x(a);
c = y(b);
r = z(b,c);
return r;
}
// functional style
func foo02(a) {
return z(x(a), y(x(a)));
}
With foo01, one can readily study the value of "b", "c", and "r" in a debugger and/or a Write() statement. With foo02 it's harder to do the equivalent. A good debugger will let you explore the output of a given function call in foo02, but you still have to rework the code to put those into a Write() statement. A direct debugger is sometimes not sufficient, especially in big loops.
It's also easier to read the first style. It's more like a bulleted list:
- Step 1
- Step 2
- Step 3
The functional style is often an awkward horizontal string of chained steps. Sure, the formatting can be adjusted, but it takes more effort, training, and/or discipline to format functional code to make it legible to ordinary code maintainers. Procedural just handles riff-raff better; functional requires ideal conditions/staff.
PS, do I really deserve a -4 score above?
- rmetzler 5y agothis is not what procedural vs. functional is. Both funcs are equivalent.
- tabtab 5y agoWhat definition of "functional" are you using?
- divs1210 5y agoBoth are functional styles. (defn foo01 [a] (let [b (x a) c (y b) r (z b c)] r)) and (defn foo02 [a] (z (x a) (y (x a)))) are both valid. I prefer the first one where I can see the intermediate steps clearly.