Imperative vs. Declarative Programming (procedural, functional, and OOP)

Imperative vs. Declarative Programming (procedural, functional, and OOP)

Why You Should Care About this Question

Maybe you heard it in an Object-Oriented Programming book. Maybe you read it on Stack Overflow. Maybe you?re reading a textbook for school.

Eventually, you get annoyed reading the terms ?declarative? and ?imperative? because you know they are important but have no clue why. In my mind, knowing the difference between imperative and declarative programming is useful for a single reason, and that is to help you better understand the difference between procedural, functional, and object-oriented programming styles. By understanding these five terms, you will be able to read through code and recognize patterns much more quickly.

The Terms

  1. Imperative Programming
  2. Declarative Programming
  3. Procedural Programming
  4. Object-Oriented Programming
  5. Functional Programming

All five of these terms are considered ?programming paradigms?, although Imperative and Declarative paradigms are parent hierarchies to procedural, object-oriented, and functional programming.

Image for post

The diagram above doesn?t make sense yet; especially because Object-Oriented Programming (OOP) is listed under both categories!

A High-Level Explanation

The difference between Imperative and Declarative programming is related to how a program works vs. what a program does. Imperative programming is about how a program works while Declarative programming is about what a program does. Let?s look at an example.

Building a House ?Imperatively?

Imperative is about the HOW. For example, if I was writing an imperative program for building a house, it would go something like this:

  1. Build the foundation
  2. Put in the framework
  3. Install the utilities
  4. Add the walls
  5. Finishing touches

In this imperative program, I have told you the exact steps to take in order to build the house. These instructions aren?t the most detailed in the world, but I have told you all the steps you need to take in order to arrive at a finished product.

Building a House ?Declaratively?

Declarative is about the WHAT. Building a house declaratively would include the following steps:

  1. I don?t care how you build it, but I want a nice fireplace, a lakefront view, and a big kitchen.

Simple right? In this declarative program, I have told you the outputs that I want. I know that if I give you inputs in the form of money, I will get the desired outputs.

It?s all about Abstraction

As programmers, we hear the word ?abstraction? a lot. Abstraction is about stripping away the nitty-gritty details in order to talk about a higher-level concept. I don?t care about all the steps it takes to get hot water to come out of my shower head. I just care that hot water comes out! This is an abstraction.

Declarative programming is an enabler of abstraction. Imperative programming is an inhibitor of abstraction. Declarative programming allows you to say ?I want this and I don?t care how I get it? while imperative programming requires you to define each and every step. If you are programming imperatively and need to print something to the console in the C language, you can write the following program:

imperative.c

In this program, I looped through each character in my string and printed it to the console using the built-in write() function. Luckily, we can just include the stdio.h library and simplify this program in a more ?declarative? way.

#include <stdio.h>int main() { printf(“Hello, World”); return (0);}

I know that reading C code isn?t the most enjoyable thing to most, but I used this example for two reasons:

  1. It demonstrates imperative vs. declarative in code.
  2. C is considered a ?procedural? programming language, which falls under the imperative category.

So let?s recap. Imperative programming is about how, and is where you list out every step of a program. It reduces abstraction to a minimum. Declarative programming is about what, and specifies the desired output without caring how the program gets to that output. It is the ultimate abstraction.

These are the top-level categories, but we can also dive one level below and talk about ?procedural? programming, which is nearly synonymous with imperative programming. Sure, there are subtleties that I?m sure plenty of people could point out, but for the desired depth of this post, we will treat imperative and procedural programming as synonymous terms.

One Big Problem

When talking about imperative vs. declarative programming, there is always the temptation to fall into a rabbit-hole of logical analysis. Let?s take another look at our ?imperative? program:

imperative-2.c

I called this program ?imperative?, but where did this write() function come from? Wouldn?t this be considered declarative? We have no idea what goes on inside that function, so we wouldn?t be correct in saying this is truly an imperative program!

And this is where the abstraction dilemma starts. We could go look inside the write() function to see how it is written, but then we would find other functions that have internal implementations. We would go at this futile exercise until we were reading the implementation of the C compiler!!

So when talking about imperative coding, we need to realize that it is a loose categorization of a much bigger idea that can be broken apart quickly if you think about it too much.

Functional Programming

Since we are talking about functions and the internals of those functions already, our transition to the concept of ?functional programming?. Similar to our discussion about how procedural and imperative programming were synonymous, we can think of functional programming as a synonym to declarative programming.

So at this point, be can assume the following:

imperative = procedural = concrete (opposite of abstract)

declarative = functional = abstract

By understanding what procedural programming is, we implicitly understand what functional programming is. It is the ability to abstract away the internals of your code. Take the following Javascript program for example:

non-functional.js

The above code works okay, but by steering towards a ?functional? approach, we might realize that Lodash (a javascript utility library) provides us a much more elegant way to implement this.

By choosing a more ?functional? way of programming, we saved ourselves 6 lines of code. One could argue that both approaches are relatively similar since the internals of Lodash do something similar to the function we wrote, but again, just like we can?t get drawn down the rabbit hole of abstraction, we can?t think too far into it here.

Why Use Functional and Procedural Programming?

Let?s get one thing straight right away?you?re not going to be using procedural programming methods much. Unless you are writing software for your custom piece of hardware and need to get into the details of memory management and all that fun stuff, procedural programming is not going to be on your radar.

So that leaves us with functional programming, which is great for the following scenarios:

  1. If the language you are using is primarily dedicated to being a ?functional? language (as opposed to object-oriented languages)
  2. If you have a fixed set of ?things? and you need to add operations to those ?things? over time

And here is where Object-Oriented programming comes into the picture. Neither of these statements will make sense until we understand what object-oriented programming is all about.

The real debate is not the merits of procedural vs. functional programming methods, but rather the merits of functional vs. object-oriented programming methods, and furthermore, why are we comparing them?

Two Camps

On the web, although improving, there is a clear ?us vs. them? mentality when it comes to functional vs. object-oriented programming. One camp believes OOP is the way forward while the other camp doesn?t believe it is ?pure? enough. Despite this, you?d have a hard time finding an Object-Oriented system that doesn?t include some sort of functional programming in it! There are subtleties you could compare, but good code isn?t about picking one or the other. Good code is about knowing when to use each type of programming.

Why use OOP?

Object-oriented programming is difficult to examine in the context of imperative vs. declarative programming because it truly doesn?t represent either. OOP is a beast of its own that tries to emulate the ?real world? through code. If you are making a web app similar to Facebook, your ?objects? could be the following:

  • Person
  • Video
  • Picture
  • Advertisement
  • Button

This is a non-exhaustive list, but the important thing to understand about OOP is that each ?object? has a set of ?methods?. Said differently, your system has nouns and verbs. A Person can addFriend(), likePost(), or updateProfile().

I have no intention of diving deep into the concepts of OOP, but we now know just enough to understand the reasons why we might use functional vs. OOP in our code.

When to Use Functional vs. OOP

Remember what I said earlier? Functional programming is great for?

  1. If the language you are using is primarily dedicated to being a ?functional? language (as opposed to object-oriented languages)
  2. If you have a fixed set of ?things? and you need to add operations to those ?things? over time

And OOP is great for

  1. If the language you are using is built to support it (i.e. Java, Python)
  2. If you have a fixed set of operations that you do on ?things?, and you need to add more ?things? over time.

Now that we have talked about OOP, we could substitute the word ?things? with ?nouns?, or ?objects?.

In other words, functional programming is great when you are building something that does not mimic the real world, while OOP is great when you are building something that may reflect the real world. For example, you might build a utility library just like Lodash (as we used earlier in a code example). The Lodash library has hundreds of utility functions, and if you tried to break it out into the ?nouns?, you might come up with the following:

  1. Array
  2. Object
  3. String

Yes, you could define these as your objects and then implement methods on them, but likely, this kind of system will be written functionally utilizing some of Javascript?s internal methods.

You have a fixed set of objects that you may add operations to. You may first release the library with 10 String operations (i.e. string.length(), string.type(), string.toJson()) and later release a version with 20 string operations.

Now if we are building Facebook, we might look towards OOP because we have a fixed number of operations on a growing set of objects. Maybe we start with a Person, Photo, and Video Object, but as the business grows, we add objects like Businesses, Advertisements, Livestreams, etc.

Conclusion

The differences between imperative, declarative, procedural, functional, and object-oriented programming paradigms are subtle, but hopefully, you have a better background to understand them from now.

With experience, you will learn which situations call for which programming paradigms, and you may even start to think about design patterns around these building blocks!

21