Skip to content

Instantly share code, notes, and snippets.

@tgalopin
Last active January 9, 2019 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgalopin/5f027c49377035a9946da2b8da15a498 to your computer and use it in GitHub Desktop.
Save tgalopin/5f027c49377035a9946da2b8da15a498 to your computer and use it in GitHub Desktop.

Answering https://twitter.com/karlhorky/status/1083028945258405888

IMO there are two main large groups of programming languages: those describing how to solve a problem and those describing the problem to solve itself.

That's my definition of declarative vs dynamic languages. A declarative language only describe what the problem is and in which context we are, then let an engine interpret these descriptions to act accordingly. There is no "logic" in the sense that in a declarative language, there is no "execution flow".

Dynamic languages, on the other hand, are much more generic: they don't describe what the problem is, they are used to express how to solve the problem. In a sense, they are closer to the hardware: you will need a dynamic language to interpret a declarative one, but you can't create a Turing-complete system without dynamic languages.

A good example of a close to declarative language is Prolog. In its core idea, Prolog is all about defining facts and letting an external engine process them to solve the problem. It's wider than that now, but originally, it was designed for this.

Other good examples besides HTML/CSS are SQL (not 100% declarative but very much so), GraphQL, etc.

I think that declarative languages are easier to learn for people not used to programming because they describe the problem to solve and not how to solve it. It's much easier to describe something than to invent something.

Back to our topic, I think we can put JS and HTML/CSS on this scale: HTML/CSS is very declarative (not 100% though, there are some kind of conditions like media queries for instance) whereas Javascript if very dynamic.

That's why I think Javascript introduces complexity compared to HTML/CSS. HTML and CSS let you describe how you want to display a page, Javascript let you manipulate data in order to display the page accordingly. The introduction of data manipulation is key here, and that's why templating engine are useful as they ease the manipulation of data (and they focus on this specifically to avoid introducing an execution flow and to stay very declarative).

JSX is a great step forward in this direction. However, it's still in the same file and in the same context as any other Javascript. It is baked inside Javascript and it lives in the middle of Javascript files. You need to understand Javascript to be able to do JSX.

IMO many PHP frameworks are a step ahead in this topic because they are designed around a strict split between the dynamic and declarative parts of a project. The view files have their dedicated directory, they are very declarative (there is usually very little logic inside) and they use strict templating engine helping maintainability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment