r/functionalprogramming • • Jun 22 '21

JavaScript Imperative vs Declarative programming in JavaScript

Hi everyone, I have tried describing the difference between imperative and declarative programming. It's not the first time that I have tried and I am hoping that I am able to communicate it in a clear way but I would appreciate the feedback of the community.

Please have a look at

the Medium post: https://medium.com/weekly-webtips/imperative-vs-declarative-programming-in-javascript-25511b90cdb7

and the YouTube video: https://www.youtube.com/watch?v=oFXOLbAX4Pw

23 Upvotes

6 comments sorted by

View all comments

Show parent comment

3

u/ragnarecek Jun 23 '21

I like the examples and explaining declarative programming is a challenge for the reasons that you describe. There is a huge difference between HTML vs SQL vs Logic vs Functional code. To make that simpler I just use declarative programming as a superset and because functional is a subset of it then what belongs to functional belongs to declarative.

Computer science is amazing and there are amazing parallels between imperative and declarative programming and because languages like JavaScript or Scala are very multiparadigmatic it opens questions that are hard to answer. For example, in functional programming, you are lead to build small atomic functions and then compose them. In imperative programming, you have a single-responsibility principle that leads you in the same direction. Or you have an inversion of control principles that lead you to build code that avoids side effects similar to pure functions in FP. But does that mean that the imperative code that follows these principles is becoming declarative?

Even researching the topic I find very different opinions on different aspects and it is not easy to decide who has the authority over it.