Sage-Code Laboratory
index<--

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. One of this paradigms is OOP = Object Oriente Programming that is addopted by Java à la carte.

Bookmarks

Linear programming

In linear programming we can have a sequence of steps that can be executed 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 exact effect when is executed. For example HTML is a language that allow linear programming.

One famost linear computer language use to be first versions of Basic. This was based on a series of statements that could have a label and a "goto" statement that could make program execution point to go back and forward in a flat program. This was efficient bat use to lead to "spagetty code" hard to debug and maintain.

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 spagetty 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 ...}.

Functional programming

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

Functional programming (FP) is a very special programming paradigm. It has a different way of thinking about a functions than the structured programming languages. In FP languages a function is an object. That’s the main idea. We do not create classes and objects like in Java. We create only functions.

Characteristics:

Note: In a functional language a function is called "first class object". That means a function is very similar to an object that is used in Object Oriented Programming. Actually in functional programming we have special design pattern to cover for lack of classes and objects.

Terminology:

High-order functionA function that manipulate other function
Call-back functionA function that is argument to another function
Lexical contextThe scope context for closures is bound and static
ClosuresFunctions defined inside of another function
GeneratorsFunctions that yield new values for each call
Recursive functionA function that call itself multiple times
Tail optimizationA recursive function that can be optimized
Lambda functionA pure function based on a single expression
Stochastic functionA function that return random results
Deterministic functionA function with the same result for the same input
Following programming languages have made this paradigm famous: {Lisp, ML, OCaml, Haskel}. Functional programming is also used in multi-paradigm languages: {JavaScript, Python, Scala, Julia, Rust, Go, Kotlin, Swift, Dart ...}.

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 polimorphism we can have a reference of an object using parent class type. If we call one method, the object method is reponding 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.

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!

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

Read next: Languages