Programming

A computer is calculator but it really is a super calculator on steroids. They are capable of performing trillions (depending on your computer) of accurate and reliable calculations per second. More than what any human being can consciously perform in their lifetime. In addition to all these, a computer can work all day all week without rest thus cheap labour.

Programming is about writing instructions for a computer in order to harness this accuracy, reliability, power and speed.

The problem however is that computer only understand binary (0’s and 1’s). This is because computers are made from transistors. Transistors can only have On or Off state. On, signifying 1’s while Off, signifying 0’s respectively. This is called Machine code or binary code.

In the early days of programming, computer programs were written in Machine Code.

Machine Code Programming was hard, just too hard.

To give you an idea:

1. Number the English alphabets 1 to 26. and let space be 27
3. My Name OPEYEMI OLORUNLEKE would be 16,15,5 . . .
4. Now convert each of those numbers to base 2 and join them together. My name would look something like this: 100001111 . . .

There you have it, you have successfully written your name in machine code*

In contrast, now imagine you want to write a program that calculates mathematical powers or Pythagoras theorem.

You guessed right, that would be a lot harder.

To address the problem, other languages were invented. After writing the code in those languages, they were later compiled to machine code, using what is known as a compiler.

Some people say writing code is like writing a work of art. Well, there’s some truth to that, you express yourself about how a problem should be solved or how a poem should be understood.

In writing, there are different styles for different settings or to express certain information. So, it is with coding. This style or way of coding is know as Programming Paradigm.

## What is a computer programming paradigm?

A computer programming paradigm is a philosophy or style of programming. It is also a way to classify programming languages based on their features.

In this article we are going to look at two paradigms, because they provide a history of programming languages from 1940 till date, and shows how OOP became the “hot thing”.

1. Paradigms based on the presence of Control Structures
2. Paradigms based on Changing a Computer State

## Paradigms Based on Control Structure

This are styles of programming that depend on whether there is presence of control structure. They are

#### What are Control Structures?

This is logic used to alter the normal flow of execution of a program. This alteration is done with the use of control transfer statements like:

1. if statements
2. switch-case statement
3. loop statement like for, while and do-while

Between 1949 and 1968, much of Computer Programming code was unstructured. This meant all computer instructions were written line by line and contained together in one file, like a shopping list.

``````// A simple "Hello World" Program written in Unstructured BASIC
100 BEGIN
101 GOTO 102
102 PRINT "HELLO WORLD"
103 END``````

A program in a non-structured language uses unstructured jumps to labels or instruction addresses. The lines are usually numbered or may have labels: this allows the flow of execution to jump to any line in the program. Look at the sample BASIC code above.

While writing computer instructions line after line (in a single file) may sound sexy, poetic and efficient, it is not!

When building a program, lines of code keep multiplying as you add more functionality. Eventually, the codebase becomes huge.

With unstructured paradigm it is close to impossible to remember the control flow of a complex program, because everything is convoluted and grouped together thus difficult to debug and modify. This is what is known as Spaghetti code.

Ah! 😩 the wonderful problem of modularity.

As a blogger, the first iteration of any article never gets published, even if it does, mistakes would need to be fixed, some parts will be remove or repositioned and so on. For example, i have revised this article more than 50 times!

My point? a program often needs to be updated to accommodate unforeseen changes or mistakes.

Computers in the 1950’s were slow, unreliable and hard to program, thus, only simple programs could be written for it but as computers started to become more powerful, non-structured programming was not going to be suitable to harness this power to solve complicated problems.

Examples of unstructured programming languages include

1. Assembly Languages
2. Early versions of FORTRAN, COBOL, BASIC and so on.

#### Example 1

We shall demonstrate cash withdrawal from a bank account by an account holder

Steps involved to withdraw money from an ATM:

1. Insert ATM Card
2. Enter Pin
3. Enter Amount
4. Collect Cash
5. Withdraw Card
``````10 PRINT “Hello there!”
20 INPUT "...Enter Your PIN...”, p\$
30 IF p\$="1234" THEN PRINT “Welcome” ELSE GOTO 20
40 INPUT "...Enter withdrawal Amount...”, a\$
50 IF a\$<2000 THEN PRINT “Collect your Cash” ELSE GOTO 40
60 END``````

In our example above, lots of features are missing. A user may not always want to withdraw money. He might want to

1. view account balance
2. deposit money
3. transfer money
4. open account
5. close account and so on.

All of these can be implemented but you would have a mess of a code that is very hard to maintain individually.

In context, you would not be able change how money is deposited without reading all of the code until you get to the part where you need to make a change. Lack of modularity is why such code hard to maintain.

A full blown banking application is far too complex for Unstructured paradigm to handle, because of lack of modularity and inability to model complex things, stay tuned for OOP.

TLDR;

What actually makes a language to be classified as unstructured is when:

1. The language is flat: it does not allowed clear blocks of codes to be defined. Blocs are a group of statements that can be replaced by another block of any size. BASIC’s line numbering does not allow this.
2. The Use of Goto statements. GOTO allows highly unstructured spaghetti code. Whatever the theory will tell you. GOTO breaks the sequence (a numbered line cannot be sure that the previous line was executed, since a direct GOTO could have lead there), GOTO does allow selection or iteration only in combination with other statements.
3. You cannot call a subroutine (aka function, method or procedure) with a simple statement,

A program of any complexity contains too many details for a human brain to comprehend without help.

To address the pitfall of non-structured programming a Dutch Computer Scientist, Edsger W. Dijkstra, published an influential albeit controversial open letter titled “Go To Statement Considered Harmful” in 1968, which led to (in addition to other stimuli) the widespread adoption of Structured Programming. The letter outlined his position on the three control structures. They are:

1. If / else / then – Conditional
2. while / for – Iteration
3. block structures and sub-routines also known as functions or procedures or methods

He is credited with coining the word Structured Programming.

According to Prof Dijkstra, the following qualities connote a Structured Programming Paradigm:

1. “Sequence”; ordered statements or subroutines executed in a particular order in which related events follow each other.
2. “Selection”; one or a number of statements is executed depending on the state of the program. This is usually expressed with keywords such as if..then..else..endif.
3. “Iteration”; a statement or block is executed until the program reaches a certain state, or operations have been applied to every element of a collection. This is usually expressed with keywords such as while, repeat, for or do..until.
4. Subroutines; callable units such as procedures, functions, methods, or subprograms are used to allow a sequence to be referred to by a single statement.
5. Blocks are used to enable groups of statements to be treated as if they were one statement.

In simpler language, a structured language should at least provide:

• blocks (without line numbers that could break the flow of control anywhere: you enter and leave a block through clear ways)
• variables that are local to a block to ensure modularity,
• clean subroutines with parameter passing,
• control flow statement that would allow to write a program without goto (even if this statement was included in the language for convenience).

Examples of Structured Programming Languages include: C, Pascal, BASIC

#### Example 2

The code below improves upon example 1

``````PRINT "Hello, Welcome to My Bank"
IF (pin\$ = "1234") THEN
PRINT "permission granted"
CALL PerfromUserPreferedAction()
ELSE
PRINT "permission denied"
ENDIF

END

SUB PerfromUserPreferedAction()
INPUT "Press 1. To Withdraw 2. To Transfer Money"; action\$
IF(action=1) THEN
CALL WithdrawMoney()
ELSE
PRINT "permission denied"
IF(action=2) THEN
CALL TransferMoney()
ELSE
PRINT "permission denied"
ENDIF

ENDIF

END SUB

SUB WithdrawMoney()
INPUT "Enter an Amount: ", amount\$
IF(amount < 2000)
ELSE
PRINT "request denied"
ENDIF
END SUB

SUB TransferMoney()
// this where you will implement the logic for how to Transfer Money
END SUB ``````

As you can see:

1. there is now code indentation: to make code more readable
2. ZERO goto statement in Place of Control Structures: to make our code more modular and easy to maintain
3. control structures: to make our code more modular and easy to maintain
4. a sub-routine: to make our code more modular and easy to maintain

## Paradigms Based on Changing machine state

This is a paradigm that is used to classify programming languages based on their ability to change a computers’ state.

They include:

1. Imperative Paradigm – in which the programmer instructs the machine how to change its state
2. Declarative Paradigm- in which the programmer merely declares properties of the desired result, but not how to compute it

#### What does “State” mean?

A computer program stores data in variables, which represent storage locations in the computer’s memory. The contents of these memory locations, at any given point in the program’s execution, is called the program’s state. As the program is executed, its state may change — variables can change and the values that are stored in memory can also change.

However, some languages do not allow the changing of a computer state. Such languages can be classified under the Declarative programming paradigm.

This is a paradigm that allows us to instruct out program to change a computer state, and they consists of:

1. Procedural Languages eg C, Fortran, Pascal etc
2. Object Oriented Languages eg C++ , Java, Kotlin etc

A procedural paradigm is a style computer programming that follows, in order, a set of commands.

They make use of procedures (also known as functions), conditional statements, and variables to create programs that allow a computer to calculate and display a desired output.

Procedures operate on data stored as variables.

Although this style of programming is modular. It is not modular enough to handle big applications and can not efficiently model complex systems. Stay tuned for OOP!

#### Example 3

The “C” code below improves upon example 2

``````#include <stdio.h>

int main(){
printf("Hello, Welcome to My Bank!");

int preferedAction = getUserPreferedAction();
if (preferedAction == 1){ //
withdrawMoney(200)
}else if (preferedAction == 2) {
bool transferMoney(200, "1234567890")
}else{
// handle the situation
}

return 0;
}

int getUserPreferedAction(){
int preferedAction = -1;
/*
Here we implement the Logic to get User Input
on the action they want to perform.
1. for Withdraw Money
2. for Transfer Money etc

and we return the action that was selected
in the form of a number.

the input the user gives us, is what we will
use to change the state of preferedAction integer
Hence, imperative.
*/
return preferedAction;
}

bool withdrawMoney(int amountToWithdraw) {
/*
Here we implement the Logic to withdraw money.
and we return if it was successful or not
We are going to assume it is always successful
*/
return TRUE;
}

bool transferMoney(int amountToTransfer, char destinationAcoountNumber[]) {
/*
Here we implement the Logic to transfer Money to another account.
and we return if it was successful or not
We are going to assume it is always successful
*/
return TRUE;
}``````

Notice something?

Yes, the code above is not different than that of Structured Paradigm in Example 2. Each functionality is still grouped in sub-routines (or procedures) but with some styling, however, the styling is not enough to earn it a Paradigm class of its own.

Procedural Paradigm is a subset of Structured Paradigm, but there is no actual difference between the two.

So what actually differentiates them?

To put it simply, imperative programming can be structured or unstructured. Structured programming can be procedural or object-oriented.

So procedural is a subset of structured and the term is usually used to contrast with object-oriented.

JacquesB – StackOverflow

#### Object Oriented Programming (OOP) Paradigm

Procedural paradigm was good. It was good enough for mid-sized applications but when it comes to large complicated real world applications (eg Enterprise Application), developers were not able to model code to mimic complicated real world systems, in a way that did not produce spaghetti code.

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).

Object Oriented Programming – Wikipedia

You could say OOP is Procedural Paradigm 2.0

It has all the good stuff from structured paradigm but with something called classes.

A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instruction to build a specific type of object.

What is Class and Object in OOP – Guru99

Classes are used to model Systems. After modeling, they are used to create objects.

An object is what is created when you instantiate a class. Instantiating a class is like giving birth to a new child. A child has exact copies of the genes of his parent and will behave like his parent. In this context the Parent of the child is a class.

Let’s revisit our bank app. The code below models the attributes (properties) of a regular bank account holder (in terms of name, etc) and the things they can do with their accounts.

``````public class BankAccount {
String accountNumber; // unique number to identify a bank account attribute
String firstName;
String lastName;
double accountBalance;
int age;
String phoneNumber;
Date dateOfBirth;

/*
all the variables above hold data that
describe a Bank Account
*/

void depositMoney(int amountToDeposit){} //methods or functions
void withdrawMoney(int amountToWithdraw){}
void transferMoney(int amountToTransfer, String destinationAccountNumber){}
}``````

#### Advantages of OOP over Procedural

The concept of classes is what makes OOP game changing.

1. It is easy to model a real system as real objects are represented by programming objects in OOP. The objects are processed by their member data and functions. It is easy to analyze the user requirements.

2. With the help of inheritance, we can reuse the existing class to derive a new class such that the redundant code is eliminated and the use of existing class is extended. This saves time and cost of program.

3. In OOP, data can be made private to a class such that only member functions of the class can access the data. This principle of data hiding helps the programmer to build a secure program that can not be invaded by code in other part of the program.

4. With the help of polymorphism, the same function or same operator can be used for different purposes. This helps to manage software complexity easily.

Large problems can be reduced to smaller and more manageable problems. It is easy to partition the work in a project based on objects.

It is possible to have multiple instances of an object to co-exist without any interference i.e. each object has its own separate member data and function.

In short, it makes programming more modular with added features.

You do not understand what polymorphism, inheritance, abstraction and encapsulation mean? Check out detailed article on Object Oriented Programming.

#### Example 4

Improving upon example 3, with the Java program, below.

Bank accounts were created for 3 people (Tom, Chris and Bailey) by instantiating the class BankAccount;

1. Tom deposits 200
2. Chris Transfers 200 to Bailey (take a close look at how bailey account number is got)
``````public class BankApplication {
public static void main(String[] args) {
BankAccount tomBankAccount = new BankAccount();
BankAccount chrisBankAccount = new BankAccount();
BankAccount baileyBankAccount = new BankAccount();

tomBankAccount.depositMoney(200);
chrisBankAccount.transferMoney(200, baileyBankAccount.accountNumber);
}
}``````

#### Similarities and Difference between OOP and Procedural Paradigm

1. Procedure and Method mean the same thing
2. A class is a unit while a module is a loose collection of functions, variables. A module cannot be instantiated. A class is a template from which objects can be instantiated.
3. An object is created from Classes. A record resembles a class but does not have function within it.

Further study: What’s So Great about OOP?

Declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow.

Declarative Programming Wikipedia

Declarative languages have no side effect, meaning they cannot change the state of a program

Declarative paradigms are known for their simple and easy to read (and understand) syntax. The reason you might want that is because some trivial things that might take many lines of code to implement in imperative programming may just take one line of code in declarative style of programming.

#### Adding Number from 1 to 10

in Java (OOP)

``````public static void main(String[] args) {

int n = 10;
int sum = 0;

for (int i = 0; i <= n; i++) {
sum = sum + i;
}

System.out.println(sum);
}``````

``sum [1..10]``

2. Database Query Languages (SQL, GraphQL etc)
3. Regular Expressions (RegEx)

#### Functional Programming

functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. It is a declarative programming paradigm in that programming is done with expressions or declarations instead of statements.

Functional programming – Wikipedia

## Summary

The most popular paradigms today are Procedural ( C ), OOP (C++, Java, Kotlin) and Functional (Haskell).

Complex Enterprise Apps and Games are built with OOP, embedded systems and low level stuff (like compilers, interpreters, device drivers etc) use procedural while in data analysis Functional is often preferred.

Modern programming languages are multi-paradigm so that you do not lack the strengths of other paradigms.

Congratulations on finishing this epic long article, thanks and come again!

## References & Credit

### Edge Developer

Hello there, my name is Opeyemi Olorunleke. I am a Software Developer (majorly Android, GitHub Profile), Digital Marketer, Udemy Instructor, Technical Writer, Blogger & Webmaster.

Close

Close