Imperative Vs Declarative Programming
Imperative programming: is like HOW you do something
Declarative programming: is more like WHAT you do.
Examples of Imperative vs Declarative
- Imperative approach: “I see that table located under the Gone Fishin’ sign is empty. My husband and I are going to walk over there and sit down.” Declarative approach: “Table for two, please.”
- I’m right next to Wal-Mart. How do I get to your house from here?
→ Imperative response: Go out of the north exit of the parking lot and take a left. Get on I-15 North until you get to the 12th street exit. Take a right off the exit like you’re going to Ikea. Go straight and take a right at the first light. Continue through the next light then take your next left. My house is #298.
→ Declarative response: My address is 298 West Immutable Alley, Eden, Utah 843
Example Programming Languages
- Imperative: C, C++, Java
- Declarative: SQL, HTML
Popular other definitions on the internet around this topic
- Declarative programming is “the act of programming in languages that conform to the mental model of the developer rather than the operational model of the machine.”
- Declarative Programming is programming with declarations, i.e., declarative sentences.
- The declarative property is where there can exist only one possible set of statements that can express each specific modular semantic. The imperative property is the dual, where semantics are inconsistent under composition and/or can be expressed with variations of sets of statements.
- Declarative languages contrast with imperative languages which specify explicit manipulation of the computer’s internal state; or procedural languages which specify an explicit sequence of steps to follow.
- In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow.
- I draw the line between declarative and non-declarative at whether you can trace the code as it runs. Regex is 100% declarative, as it’s untraceable while the pattern is being executed