Imperative versus declarative code… what’s the difference?

Martin Novak
Feb 11, 2018 · 3 min read
Imperatively 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.

About the Author

Martin Novak
www.meet-martin.com

Martin is a seasoned manager with rich experience in leading international teams and the founder of @7urtle/lambda functional programming library.

Frontend Weekly

It's really hard to keep up with all the front-end…

Martin Novak

Written by

Martin is a seasoned manager with rich experience in leading international teams and governing product strategies.

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Martin Novak

Written by

Martin is a seasoned manager with rich experience in leading international teams and governing product strategies.

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store