Imperative vs Declarative Programming with Examples

Imperative vs Declarative Programming with Examples

Imperative programming is 'how' you do something, and declarative programming is more like 'what' you do.

Let's try to explain imperative and declarative programming with examples:


Example 1: You and your husband went to a restaurant.

  • Imperative: 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: Table for two, please

Explanation: The imperative approach is concerned with how you're actually going to get a seat. You need to list out the steps, to be able to show how you're going to get a table. The declarative approach is more concerned with what you want, a table for two.


Example 2: I'm right next to Wal-Mart. How do I get to your house from here?

  • Imperative: Go out of the north exit of the parking lot and take a left. Get on I-15 going 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: My address is 298 West Immutable Alley, Eden, Utah 84310

Explanation: Regardless of how I get to your house, what really matters is the car I drive. Am I going to drive an imperative stick shift car or a declarative automatic car?


Example 3: Check the following codes


Imperative vs Declarative Programming Coding Examples


This article is adapted from here, check?


要查看或添加评论,请登录

社区洞察

其他会员也浏览了