3 ms·
> With "declarative" I still have to find out what's the intuition WHY it's supposedly better Declarative is generally better because you move complexity out o
by dempedempe 5y ago
> With "declarative" I still have to find out what's the intuition WHY it's supposedly better
Declarative is generally better because you move complexity out of your code to someone else's code. In other words, it reduces the LOC you write and have to maintain.
For example, say you are writing some code to query Postgres and aggregate a column. The imperative approach would be to query Postgres and do the aggregation yourself. The declarative approach would be to query Postgres and ask it to do the aggregation via GROUP BY.
Aside from reducing complexity, it's also usually more efficient to take a declarative approach. Dozens if not hundreds of people have been working on Postgres for almost 20 years now. The chances of your own aggregation algorithm being more efficient than Postgres's is practically zero.
- krageon 5y ago> The imperative approach would be to query Postgres and do the aggregation yourself. The declarative approach would be to query Postgres and ask it to do the aggregation via GROUP BY. These solutions don't necessarily have anything to do with the concepts as you name them.
- jasode 5y ago>These solutions don't necessarily have anything to do with the concepts as you name them. Why not? A "GROUP BY" syntax is -- relatively speaking -- more declarative than handwritten imperative code looping through a cursor and dynamically adding to a keys-values data structure to accomplish the same idea.
- majewsky 5y agoThat's not "more declarative", that's just "more high-level". Using higher-order functions does not suddenly turn imperative programming into declarative programming.
- dempedempe 5y agoA higher-order function is a function that takes a function as an argument which doesn't have anything to do with the discussion at hand. But you are right that it is higher level in that it abstracts away the logic behind GROUP BY. In this sense, all declarative programming is higher level.
- jasode 5y ago>Using higher-order functions does not suddenly turn imperative programming into declarative programming. I wasn't talking about strict definitions of "declarative programming" vs "imperative programming" to satisfy language lawyers. It was a continuation of the subthread discussion started by carlmr asking why declarative is better: https://news.ycombinator.com/item?id=30098793 https://news.ycombinator.com/item?id=30098793 So it was just talking about aspects of "declarative" in general... The idea is at a small scale, one can compare an imperative loop construct to the more "declarative" syntax that accomplishes the same task. Yes, the "more declarative" syntax will be a higher-level abstraction. Another example of the micro-instead-of-macro type of discussion was C# when it added LINQ (declarative quasi-SQL) and async/await (declarative futures). The language designers and tutorials used the word "declarative" to describe those new language features even though C#-as-a-whole is not formally categorized as a declarative language. Does using LINQ turn C# into a declarative language? No, but nobody claimed that.