Declaring Code

Imperative programming focuses on how to solve a problem, describing each step as actions. Functional programming, by contrast, is declarative. Declarative programming focuses on what is necessary to solve a problem, describing the data flow. Programming declaratively usually generates less code than programming imperatively. Less code means fewer things to write, more things done, and fewer bugs. Yay!

To see the difference between imperative and declarative, let’s look at a simple example that transforms a list of strings into uppercase. The example will be in JavaScript using the imperative mindset:

 var​ list = [​"dogs"​, ​"hot dogs"​, ​"bananas"​];
 
 function​ upcase(list) {
 var​ newList = [];
 for​ (​var​ i = 0; i ...

Get Learn Functional Programming with Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.