Why imperative code is worse than functional code

Yeah. What limits my thinking (for example with that Python code in the Beautiful thread) is destructive update: you’re two levels deep inside a data structure in a nested for; one branch of the if resets a global variable; the other branch increments it; the possibly-incremented value is updated into the data structure. Now try equational reasoning over the effect on the overall data structure.

Imperative code doesn’t have to be that confusing: don’t use global vars/always pass them as params; don’t update global data structures in situ/return the updated version (copy) from the function; apply destructive update only to local vars, where you can see their whole scope in one screenful of code.

But OOP pretty much forces you to use destructive update to invisible/abstracted elements of a data structure.

Destructive update does make sense in IO: program variables are mirroring physical changes or database changes in persistent storage outside the program run. So I love the discipline that the IO Monad imposes.

1 Like