Declarative is the new black.

The paradigm shift in the purest sense of the expression. The old paradigm makes way for new ones. And declarative is the new one. Well…, not new new

The “old” paradigm: Procedural

Procedural programming is a form of imperative programming in which you define “procedures” or “subroutines”1 that encapsulate a certain set of operations, and accept parameters or “arguments” to that set of operations. A very simple example of this, is a procedure that will return the sum of two arguments. In C:

int sum(int a, int b) {
    return a + b;
}

We declare a procedure that accepts two integer arguments a and b and return an integer sum of the arguments. Simple, efficient, clear. If we want the sum to be present in a third variable, we simply execute the procedure:

#include <stdio.h>
 
int sum(int a, int b) {
    return a + b;
}
 
int main(int argc, char *argv[]) {
    int c;
    c = sum(10, 20);
    printf("c now contains %d\n", c); /* Will output: "c now contains 30" */
    return 0;
}

Procedural is imperative

The procedural style of programming is imperative. Imperative means that you program the execution flow. You decide what pieces of code gets executed by using control structures, loops, procedures, etcetera, and the state of the variables (the “context” or “environment”) ultimately define the execution flow. This is very intuitive, since you can read most imperative languages as “if something happens, you do this and that”. More or less like an instruction video for driving. If someone comes from the right, yield.

The “newer” paradigm: Object Oriented

Another, still imperative, paradigm is Object Oriented programming. Though it is not strictly different from procedural programming, it introduces a new concept: binding data and methods to a single object, that represent one single previously initialized state of the object. This is called instantiation and means that multiple objects of one type can exist in memory simultaneously. Each of these objects have their own methods that act only on the state of that particular object.

There are two concepts that are new in OO style programming, first of which is encapsulation, where the outside world does not need to know what is going on within some object’s internals. Second is polymorphism, which means that an object can explicitly express that it is capable of some sort of functionality and may implement it in a very different way than another object which exposes the same behaviour. Huh? Bear with me. Here’s an example.

class AreaCalculator {
    public float total = 0;
 
    public void add(HasArea obj) {
        total += obj.getArea();
    }
 
    public void subtract(HasArea obj) {
        total -= obj.getArea();
    }
}
 
 
interface HasArea {
    public double getArea();
}
 
class Rectangle implements HasArea {
    private double _side1;
    private double _side2;
 
    public Rectangle(double side1, double side2) {
        _side1 = side1;
        _side2 = side2;
    }
 
 
    public double getArea() {
        return _side1 * _side2;
    }
}
 
class Circle implements HasArea {
    private double _radius;
 
    public Circle(double radius) {
        _radius = radius;
    }
 
    public double getArea() {
        return Math.PI * Math.PI * _radius;
    }
}
 
class Example {
    public static void main(String[] args) {
        AreaCalculator calc = new AreaCalculator();
        // our yard is 10 by 4.2 meters
        calc.add(new Rectangle(10.0, 4.2));
 
        // But it has a pool, 3 meter in diameter
        calc.subtract(new Circle(1.5));
 
        System.out.println("The square metrage we need to buy grass seed for is " + calc.total);
    } 
}

The result:

The square metrage we need to buy grass seed for is 27.195593

The example shows that two different objects have some common characteristic. In this case, they both know how they should produce their own area. The implementation is bound to the meaning of the class itself, since the area of a circle is calculated differently from the area of a rectangle. So, they both provide their own implementation to do so. Since the two different implementations are tightly related, it does make sense that we defined a common interface for it, so the actual consumer of the objects (the calculator) does not need to know they are totally different kinds of objects. All it needs to know is that the object is capable of producing it’s own area. That’s it. Well, if you know OO you know this already, but you might not have known that this phenomenon is called polymorphism. The fact that we hide the properties _side1, _side2 and _radius from the outside world is encapsulation. Especially polymorphism is something that is hard to achieve with a purely procedural language.

It strikes to see that OO versus procedural, however still imperative, is much more descriptive. It describes natural things in a natural way. A pear is a fruit, an apple is a fruit, they both have some specific characteristics, but they share a lot of common properties and probably operations, such as feed() or ferment(). The programming style still is imperative as it follows the execution flow, but much of the organisation of the code revolves around typing and classifying, so it is more descriptive even though not yet strictly declarative.

It would also appear that OO moves focus from processing and decision-making to behaviour. Describing behaviour is typically part of the object model, because behaviour defines how objects interact and how they depend on each other. This is curious: the data no longer plays the main role in the design of the program, the behaviour does. Let’s stash that thought.

Declarative programming

The main difference between declarative and imperative programming, is that declarative means that you express what something is, rather than what something should do. A common example of declarative programming is build scripts. I’ll use a Makefile for this example.

As we define a task within a Makefile, we can specify what tasks it is dependent on. As this simply declares what the task does, without actually doing any decision-making within the program this is typically a declarative approach. The tasks get declared as what set of executions the represent. The decisions that are needed to make for execution of the tasks is made by the platform:

# building the app depends on mysum.c (the example above)
# if mysum alters, the app should alter too
mysum: mysum.c
    gcc mysum.c -o mysum

The approach is declarative. We do no checks, we just tell what needs to be done to generate the app. See the entire Makefile for the above examples on the github repo

Another relatively simple example is Haskell. Without explaining too much about functional programming, the main idea of Haskell is that you declare anything you need. Declaring things does not mean executing them. For example:

l = repeat 5

What happens here is that we declare a list that is made up of an infinite list of 5s. So, how come nothing happens? If the list is infinite, how can it exist? It exists because it is declared. It only gets meaning as soon as it is used in context, which means that the meaning is defined by evaluating the list in it’s context. When (or even if) this happens. The problem is, imperative programming leads to the assumption that everything that is actually declared, will be evaluated as soon as execution flow enters. This is different in declarative languages, simply because they do not have execution flow. Or, at least, the execution flow is irrelevant. Evaluation is delayed until the point it is required. That is called lazy evaluation. And we like lazy, because lazy means CPU cycles and memory is only consumed as needed.

The best of both worlds

Languages now tend to shift to a mixture of (imperative) Object Oriented and (declarative) functional. We see that in Python, Ruby, Erlang and maybe even the best example of them all: Scala. What’s even more surprising is that in PHP world, which isn’t the fastest cat of the bunch, the seed is actually planted. Symfony2 introduces declarative paradigms with an inversion of control service container. That is declarative. You simply declare what service depends on another. PHP code is generated to have these dependencies resolve gracefully and lazily and we no longer need to think about execution flow for this. The services and their dependencies are only declared, and the (imperative) code for initializing the services is generated under water. Consequentally, requesting the services from the container will have the services be initialized lazily.

Another fine example is NodeJS. Since the entire model is built around one simple event loop, all behaviour is essentially defined declaratively.

So, what’s next?

Learning imperative languages will become obsolete, just as learning assembly has. You will need to learn how to define behaviour and dependencies, but no longer what needs to be done to get the entire thing rolling. As languages get more and more declarative capabilities and functional programming paradigm features, the imperative idea will only be a resort where the awesomest go.

Or…. will it? Compiling code introduces overhead. There is no compiler that is able to optimize all and every situation as well as an advanced programmer could. This holds true for, say, a C to Assembly compiler, and would most certainly be the case for compiling declarative approaches. Because ultimately, the little pieces of hardware that get the work done, are not that professed in languages. They only do ones and zeros, and have no concept of meaning. This means that bringing the code from the keyboard to the processor’s registry will become further and further apart. And if that’s the case, there’s only two ways to go. Either software will become even less efficient, or the hardware needs to get smarter. Pick one.

NB: The code in this post is on github


  1. I’m purposefully calling these “procedures” and not “functions”, as functions might confuse with functions in functional programming. 

This entry was posted in Development and tagged , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

7 Comments

  1. Posted July 19, 2019 at 12:29 | Permalink

    Yes! Finally something about Judi online terpercaya.

  2. Posted July 22, 2019 at 07:14 | Permalink

    This is very fascinating, You’re an excessively skilled blogger. I have joined your rss feed and look forward to seeking extra of your fantastic post. Also, I have shared your site in my social networks

  3. Posted July 26, 2019 at 18:17 | Permalink

    Therefore, people ingest more second online.

  4. Posted July 29, 2019 at 18:43 | Permalink

    Hello, yes this article is really pleasant and I have learned lot of things from it regarding blogging. thanks.

  5. Posted August 3, 2019 at 16:41 | Permalink

    After checking out a number of the articles on your website, I seriously like your way of writing a blog. I bookmarked it to my bookmark webpage list and will be checking back soon. Please check out my web site as well and tell me how you feel.

  6. Posted October 8, 2019 at 17:05 | Permalink

    This post gives clear idea for the new users of blogging, that truly how to do blogging.

  7. Posted October 17, 2019 at 01:05 | Permalink

    The best in class, Depoxito pay for you high-end experience that deliver the look and vibes of true VIP standarts, we give you the best fascinating to high-level experience of VIPs expect in any summit stop casino, grand enliven casino royale have enough money you the additional studio design element including the grand blackjack, offering our VIP Customer the best experience of a Salon privee table.

    New style table moreover feature across the room in imitation of grand roulette upgraded on our provider playtechs mini prestige roulette which delivering more engaging and richer playing experience. The additional experience contains a total of seven tables including five blackjack tables, one roulette table and one baccarat table. Grand conscious casino royale has been tall hand-engineered to fit the needs of our customer to using it, and contains unique elements that is specially intended to maximize the impact value we got from our customers and diversify it to the existing network.

    Soon, Depoxito will manufacture an augmented realism technology upon bring to life casino for our VIP member, these most forward looking technology ever seen in bring to life casino including this greater than before reality.

    Which permit players to experience products on an entire further level which is never seen back literally leaping out of the game and taking the blackjack, baccarat, roulette and supplementary game into the collection entire level.

    Depoxito VIP Baccarat, we pay for you the certainly exclusive stimulate VIP Baccarat that is played bearing in mind in the works to 7 players at the same table and our terribly trained lovely liven up baccarat dealer. And of course our VIP advocate will quality as if they were essentially sitting at one of the summit casino baccarat table. This immersive gaming experience creates a hugely risk-taking express that our VIP players will locate hard to surpass. Here is the list of breathing casino game that depoxito provide, we meet the expense of the widest range of live casino games on the publicize including : blackjack unlimited, blackjack prestige, roulette, baccarat, poker, hi-lo, sic bo, and grand live casino royale such as Grand Baccarat, Grand Blackjack and Grand Roulette for our VIP member. And of course as a aficionada of Depoxito you can enjoy all the games that we offer to you, every you dependence to realize is just visit our site depoxito and register it lonely takes happening to 3 minutes and after that youre customary to pretense any game that you want.

    Be our VIP, bodily our VIP fanatic of course approved you the best foster you can get from us every you craving to be a VIP believer is entirely easy. every you habit is just keep playing upon our site, buildup and do something following a VIP later than the amount that our company had written, keep playing and our customer relieve will read you that you are promoted to become a VIP aficionada upon our site.

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>