3 ms·
>Imperative code is the basis for almost everything -- whether it be OOP or functional or big ball of mud Wrong. Imperative is NOT functional. You cannot have
by deltaonefour 5y ago
>Imperative code is the basis for almost everything -- whether it be OOP or functional or big ball of mud
Wrong. Imperative is NOT functional. You cannot have both functional and imperative. They are literally opposites.
You CAN technically have both functional and OOP. However removing mutation from OOP removes the concept of setters and many associated "Design patterns" that are part of OOP.... so it's like removing the ignition from all car engines and still saying that the car is a car.
Many people don't understand what functional programming really is. Allow me to elucidate. Whatever program you can write on a single expression. That is a functional statement. If you write multiple functional statements... the code becomes imperative.
A functional program is an imperative program with ONE expression total. To write a functional program is to place your entire program ON ONE line. That's what functional is.
Now you may have heard that all you need to do is make everything in an imperative immutable for the program to be "functional." This is true. Turns out an immutable imperative program is isomorphic to a functional program. See this:
A = 2 + 2
C = A + 4 (immutable + imperative)
is equivalent to:
C = 2 + 2 + 4 (functional)
However if you add mutation the isomorphism is lost.
A = 2 + 2
C = A + 4
A = 3
You can no longer do the substitution above. Because of this isomorphism; to have any real linguistic meaning and differences between the words "functional" and "imperative" means that imperative programs MUST include reassignment. You can reassign a new value to an old variable in imperative programming and such a concept is literally impossible in a functional program because functional programs are just one line expressions.
in FP all variable names are simply aliases or macro shortcut expansions. In imperative programs variables are mutating entities.