Programming

A brief introduction to different Programming Paradigms

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:

  • Imperative Programming
  • Functional Programming
  • Object-oriented Programming
  • Aspect-oriented Programming
  • Reactive Programming
  • Functional Reactive Programming

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

  • The (first, second) are parameters of the compare method of Comparator.
  • first.length() - second.length() is the function body that compares the length of two names.
  • -> is the lambda operator that separates parameters from the body of the lambda.

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:

  • Abstraction — Process of hiding the implementation details from the user. Оnly the functionality will be provided to the user.
  • Encapsulation — Mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
  • Inheritance — Process where one class acquires the properties (methods and fields) of another. With the use of inheritance the information is made manageable in a hierarchical order.
  • Polymorphism — Ability of an object to take on many forms when a parent class reference is used to refer to a child class object.

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:

  • Cross-cutting concerns - These are aspects of a program that affect other concerns.
  • Advice - These are actions taken for a particular join point.
  • Pointcut - These are set of one or more JoinPoint where an advice should be executed.
  • Aspect - The combination of the pointcut and the advice is termed an aspect.

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:

  • Observables - Observables are the data source/stream that can emit multiple values, just one, or none. They can also emit errors and can be infinite or finite, in which case they emit their completion event.
  • Subscribers - Subscribers subscribe to Observables. They consume/observe the data and also receive the errors and completion events from the Observable.
  • Operators - They are used to create, transform, filter or combine Observables.
    • create - timers, ranges, from other data sources
    • transform - map, buffer, group, scan, etc
    • filter - filter, distinct, skip, debounce, etc
    • zip, merge, combine latest, etc
  • Schedulers - It is a mechanism that allows us to easily add threading to our Observables and Subscribers.

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

RxJavaReactive 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

Contents

Get The Best Of All Hands Delivered To Your Inbox

Subscribe to our newsletter and stay updated.