Programming Paradigm
A programming paradigm is a fundamental style of computer programming. There are four main paradigms:
- Imperative
- Declarative
- Functional (which is considered a subset of the declarative paradigm)
- Object-oriented
Declarative Programming
Declarative programming is a programming paradigm that expresses the logic of a computation (What to do) without describing its control flow (How to do). Some well-known examples of declarative domain-specific languages (DSLs) include CSS, regular expressions, and a subset of SQL (SELECT queries, for example). Many mark-up languages such as HTML, MXML, XAML, and XSLT are often declarative.
Declarative programming tries to blur the distinction between a program as a set of instructions and a program as an assertion about the desired answer.
Declarative Example in Python
# Declarative
small_nums = [x for x in range(20) if x < 5]
Declarative Example in C#
// Using LINQ for declarative programming
var evenNumbers = numbersOneThroughTen.Select(number => number % 2 == 0);
This example is declarative because we do not specify any "implementation details" of building the list. In C#, using LINQ results in a declarative style because you aren't saying how to obtain what you want; you are only saying what you want. You could say the same about SQL.
One benefit of declarative programming is that it allows the compiler to make decisions that might result in better code than what you might make by hand. For example, in SQL:
SELECT score FROM games WHERE id < 100;
The SQL "compiler" can optimize this query based on indexing and parallel execution without requiring the programmer to handle these details explicitly.
However, LINQ does not give you control over what happens behind the scenes. You must trust that LINQ will provide the requested result.
Imperative Programming
Imperative programming is a programming paradigm that describes computation in terms of statements that change a program's state. Declarative programs can be viewed as programming commands or mathematical assertions. Imperative programming is when you say how to get what you want.
Imperative Example in Python
# Imperative
small_nums = []
for i in range(20):
if i < 5:
small_nums.append(i)
Imperative Example in C#
var numbersOneThroughTen = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// With imperative programming, we'd step through this, and decide what we want:
var evenNumbers = new List<int>();
foreach (var number in numbersOneThroughTen)
{
if (number % 2 == 0)
{
evenNumbers.Add(number);
}
}
Imperative programming requires more code, but the code is testable and gives you full control over the implementation details.
Functional Programming
Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. In a pure functional language, such as Haskell, all functions are without side effects, and state changes are only represented as functions that transform the state.
Holistic Approach
All the above programming paradigms follow these general principles:
- With declarative programming, you write code that describes what you want but not necessarily how to get it.
- Declarative programming is preferred over imperative programming where possible.
- To make part of a program more declarative, other parts must provide abstraction to hide implementation details (which are imperative codes).
- LINQ is a good example: it abstracts away loops and list creations, hiding all the imperative implementation details behind the scene.
- Any program will always have both imperative and declarative code; the goal is to hide all imperative code behind abstractions so that other parts of the program can use them declaratively.
JavaScript Example
Least Declarative
var bestProducts = [];
for(var i = 0; i < products.length; i++) {
var product = products[i];
if (product.rating >= 5 && product.price < 100) {
bestProducts.push(product);
}
}
More Declarative
var bestProducts = products.filter(function(product) {
return product.rating >= 5 && product.price < 100;
});
Most Declarative (Implementation Details Hidden)
var bestProducts = getBestProducts();
By providing more abstractions, such as getBestProducts(), we can make our program even more declarative.
Want to discuss cloud architecture? Find me on LinkedIn.
Found this useful? Let's go deeper.
Book a free 15-minute call to discuss your cloud, DevOps, or AI strategy challenges.