Andrew's Digital Garden

Imperative & declarative APIs

Imperative is how you want it, declarative is what you want. If this doesn't help, declarative programming is different as it does not describe control flow.

Many (if not all) declarative approaches have some sort of underlying imperative abstraction.

Some languages and their APIs (generally): Imperative - C, C++, Java Declarative - SQL, HTML Mixed - JS, C#, Python

List<int> collection = new List<int> { 1, 2, 3, 4, 5 };

Imperative:

List<int> results = new List<int>(); foreach(var num in collection) { if (num % 2 != 0) results.Add(num); }

Declarative:

var results = collection.Where(num => num % 2 != 0);

Imperative goes to a restaurant and orders a 6oz. steak (cooked rare), fries (with ketchup), a side-salad (with ranch), and a Coke (with no ice). The waiter delivers exactly what he asked for, and he's charged $14.50. On the other hand, Declarative goes to a restaurant and tells the waiter that he only wants to pay around 12 dollars for dinner, and he's in the mood for steak. The waiter returns with a 6oz. steak (cooked medium), a side of mashed potatoes, steamed broccoli, a dinner roll, and a glass of water. He's charged $11.99

[[concepts]]

Imperative & declarative APIs