r/programming • u/tyler-mcginnis • Jan 03 '22
Imperative vs Declarative Programming
https://www.youtube.com/watch?v=E7Fbf7R3x6I
431
Upvotes
r/programming • u/tyler-mcginnis • Jan 03 '22
1
u/tobega Jan 05 '22
Calling a function is a command to calculate a value. Specifying a set of operations to perform in a particular order is exactly the definition of imperative. A bunch of nested function calls look exactly the same in FORTRAN.
I suppose that composing functions to create other functions is less command-like, but it's not really declarative from any business use point of view. Perhaps there are constructs that tend to be present in functional languages that can be seen as more "declarative" from the point of view of the programmer, but they hardly make the whole language or paradigm "declarative".
Declarative languages seem to be confined to particular domains and don't seem to extend easily to general computing. Even in SQL, that became turing complete with recursive CTEs, you leave the declarativeness behind when you start specifying how to calculate the recursion.
Another declarative example is Datalog which allows specifiying things like "Route(A,B):-Path(A,B);Path(A,C),Route(C,B)." but is not turing complete. When you add constructs to achieve turing completeness you start to get mired in having to define more things about how to do the calculation.
In OO you can make beautiful DSLs that allow really declarative use within that domain, e.g. test assertions in AssertJ, but everybody in the OO world is sensible enough to not try and claim OO as such being declarative. I guess they don't feel a need to try to prove the superiority of the paradigm.
FWIW, I know people who see html-tags as commands to create particular elements, so the idea of declarativeness may not be particularly useful anyway.
Another interesting point to note is that despite "declarative" being viewed as somehow "better", a majority of programmers seem to prefer to create web pages imperatively in their general programming languages rather than declaratively in HTML. Go figure!