DEV Community

Discussion on: Declarative vs imperative

Collapse
 
danie profile image
Daniel2222

Actually, SQL is indeed imperative, not declarative.

When you say "SELECT this and that such that bla bla bla", you're giving instructions. You're instructing to "select" (according to certain condition), and to "select" is an action.

A true declarative statement would be one expressed, for example, in first order logic. Taking on your example, where you select all the users such that their ids are < 100, in first order logic it would be:

{x / x ∈ users and x.id < 100}

That's a true declarative statement. You're saying: this is the set of persons whose ids are below to 100. You're telling the WHAT, not the HOW.

Collapse
 
mpixel profile image
Max Pixel • Edited

Indeed, and the second cake recipe is also still imperative. This would be the declarative version:

  • "Dry Ingredients" means flour + sugar + cocoa powder + baking soda, as a roughly homogeneous mixture.
  • "Batter" means Dry Ingredients + milk + vegetable oil + eggs + vanilla extract, as a well-combined mixture.
  • "Panned Batter" means a large cake pan containing Batter.
  • "Cooked Chocolate Cake" means the result of Panned Batter being in a 180°C oven for 30 minutes.*
  • "Frosting-Ready Chocolate Cake" means Cooked Chocolate Cake that is less than 32°C and not in a pan.
  • "Chocolate Cake" means Frosting-Ready Chocolate Cake that is has an even coating of chocolate frosting on it.

* Keeping "30 minutes" verges on becoming imperative. A more declarative approach to this particular part would be to specify a final moisture content, weight, or other means of determining doneness.

Perhaps it would be more declarative yet to format those steps with a more functional syntax, omitting the intermediate labels like "Batter", and using parentheses as necessary to delimit order-relevant groups. Or perhaps that would just more "functional", and equally as declarative.

I think we must admit that that there is a gradient, rather than a binary distinction, between declarative and imperative programming. The most extreme end of declarativism would be to describe the chemical structures and physical composition of the final cake, and leave it at that. But that furthest end of the declarativism gradient is achievable only in small scenarios. {x / x ∈ users and x.id < 100} is useless if users are never created (they certainly didn't exist before the big bang, and aren't timeless constructs like gravity) - in the grand scheme of things, derivation is going to need to be involved, so the program as a whole cannot be as declarative as that one snippet (the formation of users must occur before the formation of the query result). Some amount of ordering and verb choice will either be important to the author of an application, or required by the engine. Ultimately, declarative programming is not about removing all traces of ordering & verb choice from programming, but rather, it's about removing the need for incidental and inevitable ordering & verb choice from programming. What can be considered incidental or inevitable depends on the engine that evaluates the program - some chefs may implicitly know that the cake's temperature should be below the frosting's fat's melting point before it is frosted, while others need a hint.