Imperative versus declarative code… what’s the difference?

Imperative versus declarative code… what’s the difference?

Image for postImperatively speaking

As a web developer you are likely using both paradigms with different code even if you may not be familiar with the terms. Programming languages tend to have multi-paradigm tendencies using both imperative and declarative syntax and I will use JavaScript to demonstrate that as well.

Imperative paradigm

Procedural and object-oriented programming belong under imperative paradigm that you know from languages like C, C++, C#, PHP, Java and of course Assembly.

Your code focuses on creating statements that change program states by creating algorithms that tell the computer how to do things. It closely relates to how hardware works. Typically your code will make use of conditinal statements, loops and class inheritence.

Example of imperative code in JavaScript is:

class Number { constructor (number = 0) { this.number = number; } add (x) { this.number = this.number + x; }}const myNumber = new Number (5);myNumber.add (3);console.log (myNumber.number); // 8

Declarative paradigm

Logic, functional and domain-specific languages belong under declarative paradigms and they are not always Turing-complete (they are not always universal programming languages). Examples would be HTML, XML, CSS, SQL, Prolog, Haskell, F#and Lisp.

Declarative code focuses on building logic of software without actually describing its flow. You are saying what without adding how. For example with HTML you use <img src=”./image.jpg” /> to tell browser to display an image and you don?t care how it does that.

Functional programming based on lambda calculus is Turing complete, avoids states, side effects and mutation of data. You create expressions instead of statements and evaluate functions. You don?t have any use for loops and for the same argument your function will always return the same value.

Example of declarative code in JavaScript:

const sum = a => b => a + b;console.log (sum (5) (3)); // 8

Feel free to look at my post 6 fundamental terms in functional JavaScript.

In JavaScript you branch your code by either using conditional statements (if and switch) or conditional expressions (ternaries). To provide an alternative for functional programming in JavaScript I have developed NPM package conditional-expression. You can read more about it in my post: How to replace switch and ternaries in functional JavaScript.

Final thought

Real life promotes multi-paradigm approach to programming languages allowing you to take from both what you are comfortable with. Developers are usually more familiar with imperative approach rather than functional or logical approach which requires more mathematical thinking. Why should you program like a mathematician? My experience is that, you will tend to produce code which will be less prone to breaking and easier to test. However, whether you will preach imperative or declarative programming, your skills are what always matters. Study your craft.

20