An approach to SOLID Principles – Object Oriented vs Functional Programming

Reading Time: 6 minutes

SOLID PRINCIPLES

Before starting to know about what exactly the solid principles are ? We have to know about what exactly the meaning of the principle.

Principles are something which give a name of a concept so that you can talk and reason about that concept.

They help us to know that our code is in good or bad state . Principles can help you to stay out of trouble.

As a developer who cares about the software products .The main target is to create quality code throughout the development phase and want to improve the internal quality of code.

Basically , design principles provide high level guidelines to design better software applications. Those principles are named as ‘SOLID Principles’.

SOLID Principles are one of the most popular set of design principles . Promoted by American Software Engineer and Instructor Uncle Bob ( Robert C.Martin) first introduced in his 2000 paper “Design Principles Design Pattern”.

What are the oops and functional programming?

Object Oriented Programming:

Object – Oriented Pogramming is a paradigm used by developers. Which focus more on data rather than code. OOPs relies on the concept of classes and objects .Classes are the blueprint of the object , which describes how an object will behave and what an object will contain .

Objects are instances of a class that behaves as per the class has been defined. In short, Object-oriented programming is a way of giving more importance to the data than the sequence of the code.

History:

Let’s take a look at the history of oops:

Object-Oriented Programming” was coined by Alan Kay circa 1966 or 1967 while he was at grad school. Ivan Sutherland’s seminal Sketchpad application was an early inspiration for OOP. It was created between 1961 and 1962 and published in his Sketchpad Thesis in 1963.

Functional programming:

We all are aware that, Functional programming is in the trend nowadays because of its efficiency and scalability to solve modern problems.

Functional programming is the way of thinking about software construction by creating pure functions . The pure functions are those functions that never cause side effects . They always give or produce the same output of the same given input. The functional programming language focuses on the expression and the declaration rather than the execution of the statement.

NOTE: Later in this blog we are going to use  JavaScript( as a functional programming approach) which offers the functional capabilities because as we know that Js supports first-class functions.

Javascript is perfect for functional programming as it increases the performance of frameworks like Angular and ReactJS.

History:

Let’s take a look at the history of functional programming languages.

Functional programming is based on a mathematical foundation known as Lambda Calculus(which was developed by Alonzo Church in the 1930s).

The first functional programming language LISP was developed in the late 1950s for the IBM 700/7000 series of scientific computers by John Mc Carthy while as  Massachuse Institute of Technology (MIT). FP is specially designed to handle symbolic computation and list processing applications.

Some popular functional programming languages are LISP, Python, Erlang, JavaScript etc.

Need of SOLID Principles:

The goal of solid principles is to reduce dependencies so that engineers change one area of software without impacting others.

This is a simple way to improve code quality and maintainability.

Solid principles is an acronym that act as a remedy to the symptoms which shows that the software is getting rotten . Those symptoms are Rigidity, fragility, immobility .

As SOLID principles are the subset of many other principles . So I’m going to discuss some of the principles in detail.

Liskov’s Substitution Principle(LSP) :

In terms of Object Oriented Programming:

Let’s start to know about the history of lsp first. There is a lady who is a recognised computer scientist named Barbara Liskov . She is a winner of 2008 Turing award. So basically, LSP is named after  ‘Barbara Liskov.’ In 1998 paper titles “data abstraction and hierarchy ” Liksov had initially introduced the concept of underlying LSP.

Later in  1999 , the paper was restated and given a rigorous mathematical formulation. The paper restated by Barbara Liskov and Jeannette Wing titled “Behavioural Sub typing” Using Invariant and Constraints.

The LSP principle mainly focused on the relationship between super class and the subclass.

This principle is an extension of open close principle in terms of behaviour meaning that we must make sure that new drive classes extend the base classes without changing their unexpected behaviour. 

In short , the base class must be replaced by the child class.

Here is an practical example

Let’s take an example, all animals have a common behaviour i.e. to speak in a manner. But all animals sound differently such as a horse and donkey. Both are animals and have the same properties, yet technically both sound differently in reality.

Let’s take an example, all animals have a common behaviour i.e. to speak in a manner. But , all animals sound differently such as a horse and donkey both are animals and have the same properties. Yet technically both sound differently in reality. In other words, the speak method of horse and donkey prints a string ‘neigh’ and ‘hee haw’ respectively . Instances of these sub classes can be substituted where an instance of Animal is used. 

Although both return strings .If we used a jellyfish instead of a donkey then it will return an empty string as jellyfish doesn’t sound . Which means, an instance of the Animal class cannot be substituted with an instance of the Jellyfish class. Hence it violates LSV.

In terms of Functional Programming:

The Liskov Substitution Principle, this principle is common in functional programming . Where we can create a function that includes generics to ensure that one input can continuously be replaced for another without any changes to the existing code.

Here is the practical example:

Interface Segregation Principle(ISP): 

In terms of Object Oriented Programming:

Many Client specific interfaces are better than one general interface. Do not force any client to implement an interface which is irrelevant to them. ISP splits interfaces that are very large into the smallest and specific one so that clients will only have to know about the methods that are of interest to them.

     Here is an practical example

In terms of Functional Programming:

As we know that  in reality interfaces are able to make every interaction between components.

There are five SOLID Principles . Only Four principles are applicable in JavaScript i.e.SOLD . The third principle i.e. named as Interface Segregation Principle is not the applicable . Reason behind it as in this principle there is a word interface that takes some attention to it indicating the concept of the interface. So if any language has the concept of the interface then only this third principle is applicable.

For example java, c# have an interface concept so here the third principle is applicable.

In case of some languages that don’t have the concept of the interface . Then the third principle of solid is unable to use or unable to be applicable.

As JavaScript doesn’t have an interface so here the third principle is not possible to implement.

Here is the practical example:

Dependency Inversion Principle(DIP): 

In terms of Object Oriented Programming:

It is a great solution to reduce tight coupling in our software. High level modules should not depend on low level modules. Both should depend on abstraction.

Abstraction should not depend on details .Details should depend on abstraction.

For example:

TV needs a battery but it doesn’t say which brand of battery .You can use any brand of battery you want.

Here is an practical example

In terms of Functional Programming:

In functional programming, Abstractions are the default way of handling code, functions are abstractions so the dependency inversion principle is applicable here.

Here is the practicle example :

Conclusion:

So from all of this  above experience with Object Oriented and functional programming I concluded that, as a lot of  people over the world are shifting towards both the programming language by implementing SOLID principles, we can adhere to the traditional design to write code more efficiently. It will help us to organise our code properly which becomes quite scalable without downgrading the performance of the software. This will also help other developers to understand the code in a better way. Hence it improves the maintainability of the project/software as well.


Written by 

Pratibha Yadav is a Software consultant at Knoldus and started her beautiful journey of a career in an environment where she able to sharp - up her skills day by day by learning and earning new opportunities. She completed her Post-graduation from Sharda University, Greater Noida. She is passionate about her work and has knowledge of various programming languages. She is recognized as a quick learner, problem solver, public speaker, dedicated and responsible professional employee. Her hobbies are Writing, Reading, and spending some time with nature.

2 thoughts on “An approach to SOLID Principles – Object Oriented vs Functional Programming9 min read

Comments are closed.