i SC Paradigms
Sage-Code Laboratory

Programming Paradigms

A programming paradigm in Computer Science is a programming style. It consist of concepts and ideas that can lead to a better approach to create applications of quality. In this article you will learn most used programming paradigms.

Page bookmarks:

These are the programming paradigms used in diverse languages:

Info: Some computer languages are pure and implement a single paradigm and others are hybrid and implement multiple paradigms. In my opinion, multi-paradigm languages are superior but more difficult to learn. Pure languages are more easy to implement but due to many restrictions, harder to use.

Note: There are other programming paradigms, also important in computer science. We do not explain these paradmigms yet. You will learn a specific programming paradigm when you learn a programming language.

Examples:

  1. Fortran is a pure structured language,
  2. Algol is a pure procedural language,
  3. Python is multi-paradigm language,
  4. Ruby is a pure Object Oriented language,
  5. Lisp is first functional language,
  6. Haskel is pure functional language,
  7. SQL is pure declarative language,
  8. Prolog is pure logical programming language.

Linear programming

In linear programming we can have a sequence of steps that can be executed or interpreted one by one until the program is finished. The program has a single start point and execute all steps in the program. There is no exception, no jump and no skip. The program always has the same effect when is executed.

Linear programming is used in Computer Science for data oriented languages, document oriented languages and programming of old CNC machines in manufacturing industry. In the past, knitting machines used to have a pattern, that was actually a linear program that could repeat in a loop.

XML Syntax

XML stands for: Extensible Markup Language. It is a data oriented language. It is not a Turing complete language so you can not make programs to resolve computation problems. Though an XML based scripting language exist, is called Apache ANT.

XML Tag

XML Element Anatomy

Example:

In the next example you can investigate a message file, stored as XML.

<!-- secret message XML example -->
<note>
   <to>John</to>
   <from>Marica</from>
   <heading category="secret">Secret Message</heading>
   <body>Please let's meet in the balcony.</body>
</note>

XML Structure

The core idea behind XML is the markup. This are two symbols that enclose a word or so called tag that looks like this: "<tag>". XML language do not have it's own keywords pre-defined like in other languages have. Instead, every organization or developer, can define specific keywords for a particular domain. Therefore this language is called extensible, because is continuously developed and enhanced by third parties.

XML Elements

In the example above you can see the XML is hierarchic. First line is a comment. You can ignore the comments. The is a tag used to start one element. The element has an end tag . This is the end of the element. Between the start and end of an element you can store the "content" of the element, explained below:

Element Content

The content of an element can be plain text or other elements. The elements can be side by site (siblings) or nested one inside the other on any number of levels. For example <to> is a child elements of <note> that is the root element. The root element contains all other elements.

Element Attributes

The attributes of an element are enumerated after the element name before the end markup ">" separated by space. For example: category="secret" is an attribute named category with value secret. The values are always enclosed in double quotes. One element can have no attribute one or several attributes. Sometimes the attribute has no value. This is a Boolean attribute. If is present it has value True if is not present has default value: False.

Structured programming

In structured programming we create a complex, application based on nested blocks of of code called block statements. Using these structures we can avoid spaghetti code, specific to linear programming. The following structures are most popular in any imperative programming language:

  1. Decision: Create one block that is executed or not depending on one condition.
  2. Repetition: Create a block of code that is repeated several times
  3. Selection: Create a block selector based on a value with many cases

Decision:

It is usually created with "if-then-else" keywords in most languages, but also can be created with "when" keyword in Bee and EVE languages. Consist in one condition and two blocks of code. The condition can be a logical expression, but in some languages can also be an expression that is (= 0 or >0) or maybe (null and not null). You can analyze the diagram to understand how is working:

decision

Decision Diagram

Repetition:

In structured programming there are many versions of repetitive statement. The idea is simple. We start a block of code with a condition. Then we repeat the block of code until the condition become false. The condition can be a simple logical expression.

repetition

Repetition Diagram

Selection:

The selection statement has many blocks, represented in next picture by numbers: {1,2,3,4,...0}. This statement is sometimes called "switch" other times "case" or "match" or even "check". No matter how is called it does almost the same thing. It uses one value to "select" a path of execution.

switch

Switch Diagram

There are programming languages that do not implement this statement. For example "Python" do not have a selection statement. However its functionality can be simulated using "if" and "while" loop.

Usability:

Using structured programming we can create Turing complete languages. These languages can do several operations that are represented in structured programming by statements with the following properties:

  1. Start with an initial state;
  2. Can ask for input;
  3. Can change it’s internal state;
  4. Can make decisions;
  5. Can execute one sequence of code or another;
  6. Can repeat a sequence of code one or many times;
  7. Can have one or more final states;
Following programming languages has made this paradigm famous: {Fortran, Algol, Pascal, Modula, Ada}. Structured programming elements are used today in modern, multi-paradigm languages: {C, C++, Rust, Go, Scala ...}.

Object Oriented

This programming style introduce the idea that data and operations that can apply to data are tight connected. In pure Object Oriented programming we have only one things to define: Classes. Each class can hold several fields (attributes) and methods. The methods can change value of attributes.

class

Class Anatomy

Usability

You can use a class like a collection of methods. Also we can create multiple "instances" of the class. Each instance is called object. An object has same attributes ast the class and same methods. For object we can set & modify the attribute values.

To understand better we can compare the class with a cookie cutter. Each cookie we cut is an object created with the same template (the cutter). So all the cookies are almost the same, except if we sprinkle them with different color candies. Then the cookies become a little bit different from each other.

Principles

Object oriented programming has four principles, called the pillars of OOP programming. These principles are implemented by Java, Scala, Ruby a la carte. Also Python and PHP are object oriented languages.

Abstractionyou can define abstract classes and interfaces
Encapsulationyou can define public and private properties and members
Inheritanceone class can inherit properties and methods from parent class
Polymorphismone method from a parent class can be overwritten in a child class.

Abstraction

For implementing this principle, most languages enable creation of classes. This is a block of code that has a name and define a scope. In its scope you can define data (fields) and functions (methods). Together these are called "members" of the class.

Members of a class are more or less protected. You can define public members, that are visible from outside the class and private members that are visible only inside the class scope (local members).

A class, can contain logic (subroutines) that are called usually "methods". These subroutines again can be more or less visible from outside of class scope. Methods can be private or public.

Sometimes we implement methods just partial. We define "abstract" methods. A class that has "abstract" methods is also "abstract". We can not use abstract classes until they are fully implemented bu a sub-class. Check for Inheritance principle to see what a sub-class is.

Encapsulation

Encapsulation in Java is a mechanism of wrapping the data (properties and attributes) and code acting on the data (methods) together as a single unit. Declare the variables of a class as private makes encapsulation stronger.

Classes can be "instantiated". That is we clone a class, and this is done with help of a special method called a "constructor". The instances of a class are called "objects" or "instances" of the class. Each object can contain (encapsulate) different data or properties.

A class can implement special "static methods" or "static fields". These are members of the "class scope" that is unique and can be used without making an instance of the class. Static fields do not represent data but more like "class settings" or maybe "class states".

Inheritance

In languages that implement OOP a la carte, you can define one class based on other class. This mechanism is called Inheritance. All classes are derived from a primary class that is the root class. So in other words a Class can "extend" another class.

In practice sometimes this model do not work. So we need to define other concepts: "mixin", "traits", "interfaces" and "injection" that are advanced topics covered for different languages in a particular way.

Polymorphism

When we use Inheritance, a derived class "inherit" all properties and methods from base class. Sometimes we can "overwrite" a method to change the default behavior of base class. This allow us to extend and modify one class to create a different class that have similar but not exact the same behavior. This is called polymorphism.

To take advantage of polymorphism we can have a reference of an object using parent class type. If we call one method, the object method is responding and not the parent class method. This way the method become polymorph. It changes with the object type.

Advanced Features

Diverse OOP languages may offer other features. These features enhance even farther the capability of programmers to create reusable and reuse code made by other developers. However, learning these features require additional effort. Let's define these features:

Generics

Generics are classes that are not very specific for a particular data type. For example a dynamic list has the same logic but can apply to different primitive types: List of integers, List of floats and List of Double. In this case we can use a generic library to create a specific List.

This mechanism was created to relax a little bit the strong typing paradigm. It reduces the required lines of code we need to write for a specific design pattern. For dynamic programming languages this is not necessary since a particular object or data member can have any data type.

Interfaces

Interfaces are "abstract" concepts that force a specific behavior. Interfaces are almost the same as "abstraction" except that one class can implement multiple interfaces but can extend only one abstract class. So that's the main difference.

An interface use to have all methods abstract in older Java versions. Now you can create methods that have a default implementation using an interface. However most developers are using abstract method declarations for interfaces.

Mix-ins and Traits

Some computer languages that are Object Oriented enable you to inherit new features from multiple base classes. This is called multiple inheritance. Most common though, OOP languages like Java, Scala and Ruby do not support multiple inheritance. Another way was found to enable developers reuse code from multiple ancestors.

A mix-in is a class that re-use multiple "components" to simulate multiple inheritance. The components are called "traits", and represent namespaces. You can include multiple traits in a class to inherit all methods and variables defined in a trait. In Ruby there are no traits but modules.

Following programming languages have made OOP paradigm famous: {SmallTalk, Java}. Functional programming is also used in multi-paradigm languages: {C++, Python, Scala, Dart, Ruby...}.

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 emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state.

Some of the key concepts and terms belonging to functional programming:

Disclaim: These are concepts I have understood so far. There are many more concepts to learn. Check the references below to learn more. Of course, my article is not a full course for functional programming but an introduction.

Every functional programming language need function to be powerful enaugh to support this paradigm. Before going in details let's learn about the functions.

Function

In programming a function is a named block of code designed to perform a particular task. Functions usually have one or more parameters and most function return one result. The function is a common way to create reusable code and split a larger problem into smaller parts.
function

Function Concept

Note: Functional programming was first used by Lisp and is very old. It was invented by John McCarthy in 1960. It was not understood correctly at that time and ignored by most others for many years.

Languages

Functional programming is back and stronger than ever. It is superior to Object Oriented paradigm but harder to understand at first. Therefore new programming languages are combining Object Oriented, Functional Programming and Structured Programming.

Functional programming languages are designed on the concept of mathematical functions that use conditional expressions and recursion to perform computation. Unlike classic programming, functions are “first class citizens”. All sub-programs are functions. Some of the popular functional programming languages include:

Scope Model

Fundamental difference between programming languages is the scoping model used to define variables and constants. There are 2 models known and used in programming:

Dynamic Scope is created at execution time while Static Scope is created with the function and remain in memory as long as function is available. This approach makes functions behave like objects. So in functional programming, functions are called “first class citizens”. Functions can have states. That is the main difference between structured programming and functional programming.

Functional Features

Using static scope enable some interesting features that makes functional programming exciting and efficient. Some of these features are:

Advantages

Functional Programming is more efficient and more safe especially useful for concurrent programming. Modern computers are using multi-core processors that enable execution of functions in parallel to improve performance. This is why modern programming languages are using Static Scoping and Functional Programming.

Pure Languages

Pure functional languages are harder to accept. Some languages that I have study are very different than classic languages. There are some special features that are more than I can handle. Some features of functional programming is implemented in so called hybrid languages or multi-paradigm languages, that I like more.

In these languages functions do not have unexpected “side effects” that could potentially modify or influence the result or the behavior of other functions. However these languages do not have classic control flow statement like loops and selection statements. Instead you can use expressions and recursive functions to resolve problems and this was difficult for me.

Modern Languages

Modern programming languages are using Static Scoping. This make functions more deterministic but enable control flow statements to be used inside functions. My favorite new languages are these:

Note: In last versions of Java we can see introduction of functional programming. Now Java has support for lambda functions, very useful for creation of so called “call-back functions”, used in generic algorithms and parallel computing. There are two Java alternative languages that implement functional programming paradigm fully and can also run on JVM: Kotlin & Scala. Both are famous languages used in production.

References

A comprehensive glossary:fp-glossary

Conclusion:

There are advantages and disadvantages of each programming paradigm. Therefore many computer languages implement multiple paradigms. Personally I find this a good thing. Now you are prepared to study about history, present and future of computer languages. Congratulation. Read on!


Read next: Programming Languages