What exactly is Programming Paradigm?

A programming paradigm is a style or way of programming which defines how we think and approach the problems. There can be more than one type programming paradigms. Here is a brief list of the common ones:

There are lots of programming languages that use one or some of these strategies when they are implemented. And that strategy is a paradigm.

Let’s briefly go through each one of these.

Imperative Programming

Imperative programming is the style of programming in which there is a sequence of statements that change the state of the program. Control flow in imperative programming is explicit i.e, commands show how the computation takes place, step by step. Each step affects the global state of the computation.

int grandtotal = 0;
int a = 1;
int b = 5;

grandtotal = a + b;

Sysout(grandtotal);

Imperative programming says how to do something. As shown above, we can see the state of grandtotal changed from 0 to 6 by some series of sequential commands. This makes the programs simpler for a someone to read and write. Imperative programming can be written almost in every programming language.

Functional Programming

Functional Programming is about avoiding reassigning variables, avoiding mutable data structures, avoiding state and favouring functions all-the-way. It is mainly used to perform mathematical functions or logical operations that doesn’t change the state of variables defined in the program.

Functions implemented should be idempotent by nature i.e, make that same call repeatedly while producing the same result.

Functions are classified as Pure, Impure or Higher Order Functions.

Pure Functions

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

This is an example of Pure Functions. As shown above, sum() method/function only depends on the input parameters and will not have any side effects modifying the state of variables that are defined outside of the function.

Impure Functions

public class ObjectWithImpureFunction{
  private int value = 0;

  public int add(int nextValue) {
    this.value += nextValue;
    return this.value;
  }
}

Notice how add() method uses a instance/member variable to calculate its return value, and it also modifies the state of the member variable, so it has a side effect. This state changing makes this function to be termed as Impure.

Higher Order Functions

Higher order functions are functions that either accept other functions as arguments or returns a function as a result. With the introduction of Lambda expressions in Java 8, Java supports higher order functions.

List<String> names = Arrays.asList("Madan", "Meenakshi", "Manya");
Collections.sort(names, (first, second) -> first.length() - second.length());

As shown above, sort function is an example of a Higher order function that accepts a lambda expression. The expression (first, second) -> first.length() - second.length() is a lambda expression of type Comparator<String>.

Object-oriented Programming

Object Oriented Programming a.k.a OOP is the most popular programming paradigm. It has unique advantages like the modularity of the code and the ability to directly associate real-world business problems in terms of code. It proposes the creation of templates (or classes) which can define the state and behavior of the instances they model. The instances interact with each other by sending them messages via methods.

This programming paradigm defines four principles:

Aspect-oriented Programming

Aspect Oriented Programming a.k.a AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns.

It does so by adding additional behaviour to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a pointcut specification, such as “log all function calls when the function’s name begins with ‘set’“. This allows behaviours that are not central to the business logic (such as logging) to be added to a program without cluttering the code, core to the functionality.

One can think of AOP as a debugging tool or as a user-level tool. Advice should be reserved for the cases where you cannot get the function changed (user level) or do not want to change the function in production code (debugging).

Standard terminology used in Aspect-oriented programming may include:

Reactive Programming

Reactive programming is all about handling asynchronous streams of data. It is concerned with data streams and the propagation of change.

In reactive programming, Observables emit data, and send it to the subscribers. This can be seen as data being PUSHed in reactive programming, as opposed to data being PULLed in imperative programming, where you explicitly request data (iterating over collection, requesting data from the DB, etc).

Building blocks of Reactive Programming:

The

Reactive Manifesto
is a document that defines the core principles of reactive programming.

RxJava
Reactive Extensions
for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

Functional Reactive Programming

Reactive programming, with the concept of Functional programming is termed as functional reactive programming a.k.a FRP.

FRP helps us to think about asynchronous programs (high-level abstractions), makes the flow of your application easier, and improves standard error handling (data structure = less code, less bugs). That is the reactive part. The functional part is the reactive extensions. Rx allow you to manipulate and combine streams of events. Together, that is really the power of functional reactive programming: the ability to combine functions, operate, and transform the stream of events.

Resources