Imperative/Declarative

We learn about imperative and declarative sentences in English class:

This is imperative: “Make a pie.” Imperative sentences give orders.

This is declarative: “A pie has a crust and some filling.” Declarative sentences state information.

We can think of code this way, too:

Imperative means I’m giving the computer orders, a series of steps to complete.

Declarative means I’m giving the computer information, defining rules and logic.

Of course, making the computer do anything useful tends to require rules and logic and a series of steps to follow to apply those rules. We need both kinds of code.

But when we design our software, we can choose to separate the two kinds of code. We can write declarative code for the rules and imperative code for the steps.

Why would we want to do that? Because when we need to read or to change or to test our code, it’s way easier with a rule than with a series of steps.