Declarative Versus Imperative Code

khaladahasan
6 min readNov 27, 2020

Believe it or not, you are likely already using multiple programming Paradigms as a developer. Because there is nothing more fun than

Ntertaining your friends with programming theory, here is an article that will help you recognize popular paradigms in your code
Imperative Code
Imperative programming is how we started with Assembly (1949) and continued with languages like C, C++, C#, PHP, and Java. Procedural and object-oriented programming belong under the imperative paradigm.
Your code is based on statements that change the program state by telling the computer how to do things. In other words, your code is based on defining variables and changing the values of those variables.
It is the ideal programming paradigm for people who have a less-than-innocent fetish for telling machines how to do their thinking.
Procedural code
Procedural code uses procedures to manage its structure. A procedure is simply a set of actions run in a specific order that you are able to call repeatedly instead of jumping by using goto commands.

The above example in C language reads three numbers, uses pointers, and through conditional logic drives the program flow to determine the largest number out of the three. Procedural languages like C can offer you a fairly simple and very computing efficient solution to application challenges.
You usually need to read each procedure from top to bottom to understand what it does. Procedural programmers can be sometimes criticized for a tendency of writing spaghetti code. But any college student who hasn’t been dependent on spaghetti with ketchup for dinner had it easy in life.
Examples of procedural programming languages are Pascal (1970) and C (1972). Procedural programming has strong support. Linus Torvalds, the father of Linux, has been quite open in his criticism of C++ and object-oriented programming.
Object-oriented code
Object-oriented programming is modeling objects that hold internal states. The programming code is then based on the relationship among these objects. In classed-based languages, objects are instances of classes.
The code in the methods of the objects is still imperative and based on statements that modify the state.

https://www.cratesboats.com/sites/vtube/Br-vs-Ra-Q1K.html
https://www.cratesboats.com/sites/vtube/Br-vs-Ra-Q2K.html
https://www.cratesboats.com/sites/vtube/Tranmere-vs-Brackley-liv-QQ1.html
https://www.cratesboats.com/sites/vtube/Tranmere-vs-Brackley-liv-QQ2.html
https://www.cratesboats.com/sites/vtube/Tranmere-vs-Brackley-hd-01.html
https://www.cratesboats.com/sites/vtube/Tranmere-vs-Brackley-hd-02.html
https://www.justlabels.co.za/cmc/video-crystal-palace-newcastle-Q1.html
https://www.justlabels.co.za/cmc/video-crystal-newcastle-Qu1.html
https://www.justlabels.co.za/cmc/video-crystal-newcastle-liv1.html
https://www.justlabels.co.za/cmc/video-crystal-newcastle-cmc1.html
https://www.justlabels.co.za/cmc/video-crystal-newcastle-cmc2.html
https://www.mercurycorp.com/jeds/telor/video-Crystal-Palace-Newcastle-v-en-gb-P7f2.html
https://www.cratesboats.com/sites/vtube/video-Crystal-Palace-Newcastle-v-en-gb-1yri.html
https://www.cratesboats.com/sites/vtube/video-Crystal-Palace-Newcastle-v-en-gb-P7f.html
https://www.cratesboats.com/sites/vtube/video-Crystal-Palace-Newcastle-v-en-gb-P7f1.html
https://www.cratesboats.com/sites/vtube/UM-v-Lib5.html
https://www.justlabels.co.za/hty/video-Crystal-Palace-Newcastle-v-en-gb-1olp.html
https://www.justlabels.co.za/hty/video-Crystal-Palace-Newcastle-v-en-gb-1ply.html
https://www.justlabels.co.za/hty/video-Crystal-Palace-Newcastle-v-en-gb-1tpm.html
https://www.justlabels.co.za/hty/video-Crystal-Palace-Newcastle-v-en-gb-1trn.html
https://www.justlabels.co.za/hty/UM-v-Lib6.html
https://www.mercurycorp.com/jeds/telor/UM-v-Lib1.html
https://www.mercurycorp.com/jeds/telor/UM-v-Lib2.html
https://www.mercurycorp.com/jeds/telor/UM-v-Lib3.html
https://www.mercurycorp.com/jeds/telor/UM-v-Lib4.html
https://www.justlabels.co.za/hty/Neb-v-Iow2.html
https://www.justlabels.co.za/hty/Iow-Stat-v-Tex-nca3.html
https://www.justlabels.co.za/hty/Iow-Stat-v-Tex-nca4.html
https://www.mercurycorp.com/jeds/telor/Neb-v-Iow-1plu.html
https://www.mercurycorp.com/jeds/telor/Iow-Stat-v-Tex-nca-1pol.html
https://www.mercurycorp.com/jeds/telor/Iow-Stat-v-Tex-nca2.html
https://www.cratesboats.com/sites/vtube/Neb-v-Iow1.html
https://www.cratesboats.com/sites/vtube/Iow-Stat-v-Tex-nca1.html
https://www.cratesboats.com/sites/vtube/Iow-Stat-v-Tex-nca-1opl.html
https://www.mercurycorp.com/jeds/telor/Liberty-vs-UMass-ncaaf-01.html
https://www.mercurycorp.com/jeds/telor/video-Liberty-vs-UMass-ncaaf-02.html
https://www.mercurycorp.com/jeds/telor/video-Liberty-vs-UMass-q1.html
https://www.mercurycorp.com/jeds/telor/video-Liberty-vs-UMass-q1.html
https://www.mercurycorp.com/jeds/telor/video-Liberty-vs-UMass-q2.html
https://www.cratesboats.com/sites/vtube/Texas-vs-Iowa-State-livQQ1.html
https://www.cratesboats.com/sites/vtube/Texas-vs-Iowa-State-livQQ2.html
https://www.cratesboats.com/sites/vtube/Texas-vs-Iowa-State-livQ0.html
https://www.cratesboats.com/sites/vtube/Texas-vs-Iowa-State-li.html
https://www.justlabels.co.za/cmc/Nebraska-v-Iowa-1.html
https://www.justlabels.co.za/cmc/Nebraska-v-Iowa-liv1.html
https://www.justlabels.co.za/cmc/Nebraska-v-Iowa-liv2.html
https://www.justlabels.co.za/cmc/Nebraska-v-Iowa-rwe1.html
https://www.justlabels.co.za/cmc/Nebraska-v-Iowa-rwe2.html
https://www.mercurycorp.com/jeds/telor/video-UNC-Notre-10.html
https://www.mercurycorp.com/jeds/telor/video-UNC-Notre-20.html
https://www.mercurycorp.com/jeds/telor/video-UNC-Notre-30.html
https://www.cratesboats.com/sites/vtube/UMass-v-Liberty-ncf04.html
https://www.cratesboats.com/sites/vtube/Iowa-State-v-Texas-ncaaf04.html
https://www.cratesboats.com/sites/vtube/Nebraska-v-Iowa1-ncaf04.html
https://www.cratesboats.com/sites/vtube/ncf-umas-v-liberty-04.html
https://www.cratesboats.com/sites/vtube/Iowa-state-v-texas-ncaf-wk12.html
https://www.cratesboats.com/sites/vtube/videos-tex-v-low-j09.html
https://www.cratesboats.com/sites/vtube/videos-umass-v-librty-m01.html
https://www.cratesboats.com/sites/vtube/videos-tex-v-low-nca-j10.html
https://www.cratesboats.com/sites/vtube/nebska-v-Iowa-ncaf09.html
https://www.mercurycorp.com/jeds/telor/UMass-v-Liberty-ncf05.html
https://www.mercurycorp.com/jeds/telor/Iowa-State-v-Texas-ncaaf05.html
https://www.mercurycorp.com/jeds/telor/Nebska-v-Iowa-ncaf05.html
https://www.mercurycorp.com/jeds/telor/xyz-Nebska-v-Iowa-ncaf06.html
https://www.mercurycorp.com/jeds/telor/Iowa-State-v-Texas-ncaaf06.html
https://www.mercurycorp.com/jeds/telor/umass-v-liberty-ncaf06.html
https://www.justlabels.co.za/hty/video-umass-v-liberty-ncaf07.html
https://www.justlabels.co.za/hty/video-Iowa-State-v-Texas-ncaaf08.html
https://www.justlabels.co.za/hty/video-nebska-v-Iowa-ncaf07.html
https://www.justlabels.co.za/hty/Iowa-State-v-Texas09.html
https://www.justlabels.co.za/hty/umas-v-liberty-ncaf08.html
https://www.justlabels.co.za/hty/Iowa-state-v-texas-nca20.html
https://www.justlabels.co.za/hty/nebska-v-Iowa-ncaf08.html
https://www.justlabels.co.za/hty/umas-v-liberty-ncaf09.html
https://www.cratesboats.com/sites/vtube/ncf-umas-v-liberty-nca05.html
https://www.cratesboats.com/sites/vtube/tex-v-low-nca-j20.html

The above is an example of a factory design pattern implemented in Java. Notice how all the code focuses on defining classes and using a relationship among them through an interface. Usually, all these classes would be separated out into their own files.
Examples of object-oriented languages are Smalltalk (1972), C++ (1985), Python (1991), Java (1995), JavaScript (1995), Ruby (1995), C# (2000), Scala (2004), and Swift (2014).
JavaScript started as a prototype-based, object-oriented language without classes. Classes were added in ECMAScript 6 in 2015.
When you are studying programming, chances are that, of all the paradigms, you will encounter object-oriented code first and most. Even though it is the most popular paradigm, it still meets with serious criticism for its complexity, difficulty of understanding design patterns, and tricky state management across the application. At the same time, object-oriented programming languages are very mature and professionally appreciated.
In JavaScript, the front-end framework Angular is a great example of C# having a big influence on developers wanting to bring stronger object-oriented principles into web development.
Please continue using object-oriented programming in all your projects. Because if you don’t, all the people that know only this one paradigm will suddenly be out of a job and live in cardboard boxes. And you don’t want to be responsible for that, do you?
Declarative Code
Declarative code is very common, and it is represented by domain-specific, logic, and functional programming languages. Examples of these are HTML, SQL, F#, Prolog, and Lisp.
Declarative code focuses on expressions that are saying what without adding how. For example, HTML code <img src=”./turtle.jpg” /> tells the browser to display an image of a turtle without telling the browser how. It is also typical that a declarative code avoids mutation of state and variables.
Domain-specific code
Domain-specific languages are not Turing complete, which means that they can’t do everything that other Turing-complete languages can. For example, C# (imperative) and F# (declarative) are both Turning-complete languages, and everything that you can develop in one, you can also develop in the other one. HTML is not Turing-complete and allows you to do only specific things.
An example of SQL code that finds employees and their managers in a database:

Domain-specific languages are usually very simple to write and read. Because of that fact, they are the most popular way of user interface declaration. For example, the JavaScript programming library React uses JSX for defining components:

Examples of domain-specific languages are SQL (1974), HTML (1993), CSS (1996), and XML (1996).
Logic code
Logic programming is a declarative paradigm based on formal logic. A program is a set of sentences in a logical form that express facts and rules about some problem domain.
The most typical logic programming language that is Turing-complete is Prolog (1972). That means that everything that you can write in C language you can in theory also write in Prolog.
Prolog is based on defining predicates that define the relationship between their arguments.

In the example above, you define facts that result in true and then you ask questions that again result in true or false boolean.
Remember that turtles are not food and someone should tell that to the starving people on Naked and Afraid.
Prolog is kinda magical when you work in it, and if you disagree then you are an evil baby-eater anyway.
Functional code
Functional programming is a declarative paradigm based on a composition of pure functions. Functional programming languages are Turing complete and based on lambda calculus, which is a system of mathematical logic from the 1930s.
A pure function is a function that depends only on its input and always provides an output without mutating or reading any external state. That is very different from procedural programming. Function composition is then about using simple functions together in a sequence to build your code.
Functional programming has been growing steadily in its popularity in the past years, and it has made its way into imperative programming languages. That means that languages such as Python, C++, and JavaScript are multiparadigmatic as they support writing code in multiple paradigms.
Here is an example of a functional code written in JavaScript using the @7urtle/lambda library:

Functional programming brings a number of new concepts that are not present in object-oriented programming like pure functions, higher-order functions, immutability, functors, partial application, point-free functions, and so on. Because of that, the barrier of entry can be seemingly high, especially since many functional programming articles like to go very deep into its mathematic roots. I recommend having a look at simple articles like @7urtle/lambda JavaScript functional programming basics for the introduction.

--

--