DEV Community

Discussion on: Imperative vs Declarative Programming

Collapse
 
awwsmm profile image
Andrew (he/him)

Imperative and declarative programming are similar to the imperative and declarative moods in English writing. The imperative mood is basically for issuing commands like "sit down" or "be quiet". The declarative mood indicates what is occurring, without strictly defining the steps to make that action occur ("John is walking to the store" rather than "left foot, right foot, left foot, ...").

Imperative programming tends to be low-level, specifying exactly what should be done, step-by-step. Declarative programming tends to be higher-level, specifying the outcome without necessarily dictating how that outcome should be achieved.

An example imperative snippet:

int temp = 0;
for (int ii = 0; ii < array.length; ++ii) {
  temp += array[ii] * array[ii]
}
cout << temp << endl;
Enter fullscreen mode Exit fullscreen mode

An example declarative snippet:

println(array.map(x => x*x).sum)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kamtoeddy profile image
Kamto Eddy
int ii = 0;
Enter fullscreen mode Exit fullscreen mode

This got me paranoid for a sec 😅
I'm so used to i 😂

Collapse
 
awwsmm profile image
Andrew (he/him)

Yeah I use double-indices like this because it makes it easier to ctrl-F for them!