3 ms·
I've seen some imperative code in the wild that looks like this: bool go = true; if (go) doA(&go); if (go) doB(&go); if (go) doC(&go); As a fu
by Twisol 4y ago
I've seen some imperative code in the wild that looks like this:
bool go = true;
if (go) doA(&go);
if (go) doB(&go);
if (go) doC(&go);
As a functional thinker, this is "just" an encoding of the Maybe monad, so I actually quite like it. If you care about small efficiency wins, though, it's not great -- an early bail means checking all the remaining conditions. It's cute though!
I'm generally pretty okay with similar patterns over accumulators, as long as it's clear that the purpose of that variable is to be an accumulator. If we're just overwriting a variable because we don't need the old value anymore, or something like that, I'm much less charitable.
A big benefit of functional programming, for me, is to learn the safe roads as explicitly as possible, so that you can then identify (and use) them when they aren't so well signposted.
- deleted 4y ago[deleted]