Most developers are very familiar with writing imperative programs (even though they may not know it by that name). In this article, I will introduce you to an alternative style of programming called declarative programming. Proper declarative programs are easier to read, understand, and maintain.
As professionals, we should be striving to write better programs each day. If you cannot look at programs you wrote three months ago with a critical eye and notice things that could be better, then you have not improved and are not challenging yourself. I challenge you to write programs that are easier to read and understand by using declarative programs.
- What Is An Imperative Programming?
- What Is A Declarative Programming?
- Declarative Programming Examples
- Further Reading
First, it is important to understand what declarative programs are and how it relates to imperative programs.
To understand this we have to first understand what is imperative programming.
What Is An Imperative Programming?
In computer science, imperative programming is a programming paradigm that uses statements that change a program’s state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform.
Imperative programs describe how something is done whereas declarative programs describe what is being done.
Imperative programs are generally difficult to read and understand. For example:
It is not immediately apparent what this program does. Only through careful examination can we deduce that it prints the sum of all even numbers between 0 and 99.
Now, let us see how declarative programming will help us out here,
What Is A Declarative Programming?
In computer science, declarative programming is a programming paradigm—a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.
This is how the above program would be implemented using a declarative style:
Obviously, the second example is different, but is it better? I believe it is. We have condensed the example into a single expression and the expression is significantly easier to understand. The name of each method is used to express the intention of that portion of the program. Rather than looping with a classic for-loop I have used the newer
Enumerable.Range method. This not only better expresses my intentions but also gives me a starting place from which I can easily stream the numbers through a filter (the Where method) and finally aggregate them with Sum.
But I still think this code could be better. Let me do one last thing to make our program even more declarative:
This change is subtle but important. We have moved the somewhat cryptic expression that tests for evenness into its own method. Since this method has a single responsibility and is clearly named, it is ideal for inclusion in our declarative expression. It is important to understand that declarative programs don’t necessarily mean fewer lines of code. Declarative programs are characterized by how expressive it is. By moving the test for evenness into its own method we may have increased the line count of the program, but we have also greatly improved the readability of the programs as well.
Declarative programming is nice because it can help simplify your mental model* of code, and because it might eventually be more scalable.
I am a .NET developer and if you are too, you will find this kind of programming style everywhere in .NET.
For example, let’s say you have a function that does something to each element in an array or list. Traditional code would look like this:
No big deal there. But what if you use the more-declarative syntax and instead define
DoSomething() as an Action? Then you can say it this way:
This is, of course, more concise.
Declarative Programming Examples
Below are some examples I found from my peers:
I hope that I have shown how you can improve your programs by making it more declarative. If you strive to write more declarative programs you will end up with better software that is easier to read, understand, and maintain.
If you are already doing it do send me your declarative style examples in the comment below and I will love to add those up in my article.
What is Functional Programming? by Kevin Sookocheff - This article explains functional programming paradigm and explains how functional programming is a form of declarative programming that expresses a computation directly as a pure functional transformation of data.
What the Heck is Declarative Programming, Anyways? by Keith Brewster - This article discusses about declarative programming in the form of lessons that answer the most common question regarding declarative programming.
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox