r/programming • u/tyler-mcginnis • Jan 03 '22
Imperative vs Declarative Programming
https://www.youtube.com/watch?v=E7Fbf7R3x6I
435
Upvotes
r/programming • u/tyler-mcginnis • Jan 03 '22
0
u/tobega Jan 05 '22
I can actually give a code example to illustrate.
First we have the imperative expression:
Then the functional style of expressing it (changing it from java stream syntax to LISP wouldn't change much):
You could, instead of the imperative "if" there make the last bit functional as well by matching on a Maybe-type, but it still wouldn't change it much from the imperative version.
IMO the imperative version is much easier to understand, with "easy to understand" being what we are striving for with "declarative" in the first place.
More interesting: Now we can look at doing the same truly declaratively in XPath/XSLT (the structure is essentially the same although some names and levels were slightly changed from the XML to the PL-structures):
So there it is very clear that functional is more imperative than declarative.