Functional Programming VS Object Oriented Programming
Introduction
Two of the most important programming paradigms today are functional programming,, and object-oriented programming.
Functional Programming
Functional programming is a type of programming that focuses on evaluating functions and creating the structure and parts of computer programs .It is not about executing commands or statements, but about evaluating expressions, it also provides excellent support for structural programming. The objective of any Functional Programming language is to mimic the mathematical functions. However, the basic process of computation is different in functional programming. Some most prominent Functional programming languages are Haskell, SML ,Clojure ,Scala ,Erlang ,Clean ,F# ,XSLT ,SQL and Mathematica.
Functional programming concepts
1) Immutable Data
Immutable Data means that you should easily able to create data structures instead of modifying ones which is already exist.
2)Referential transparency
Referential transparency means that the function will always return the same value given the same set of inputs.
3)Modularity
Modular design increases productivity. Small modules can be coded quickly and have a greater chance of re-use which surely leads to faster development of programs
4)Closure
The closure is an inner function which can access variables of parent function’s, even after the parent function has executed.
5)Higher-order functions
Higher-order functions either take other functions as arguments or return them as results.
6)Pure function
A ‘Pure function’ is a function whose inputs are declared as inputs and none of them should be hidden. The outputs are also declared as outputs.
example:
Function Pure(a,b)
{
return a+b;
}
7)Impure functions
Impure functions exactly in the opposite of pure. They have hidden inputs or output; it is called impure. Impure functions cannot be used or tested in isolation as they have dependencies.
Example
int z;
function notPure (){
z = z+10;
}
8)Function Composition
Function composition is combining 2 or more functions to make a new one.
Application of Functional Programming
- mathematical computations.
- developing a variety of commercial and industrial applications.
Advantages of Functional Programming
- Easier to design -Everything is a concise module.
- Immutable variables lead to fewer side-effects.
- It’s easily testable /debuggable .
- Development is faster.
Disadvantages of Functional Programming
- Slow execution.
- Coding difficulties.
- Typically less efficient.
Object Oriented Programming
Object-oriented programming (OOP) is a computer programming model that organizes software design around data, or objects, rather than functions and logic. There are many object-oriented programming languages including JavaScript, C++, Java, and Python. OOP can be used for manufacturing and design, as well as mobile applications
The structure of object-oriented programming include the following:
- Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods.
- Objects are instances of a class created with specifically defined data. Objects can correspond to real-world objects or an abstract entity.
- Methods are functions that are defined inside a class that describe the behaviors of an object. Each method contained in class definitions starts with a reference to an instance object.
- Attributes are defined in the class template and represent the state of an object. Objects will have data stored in the attributes field. Class attributes belong to the class itself.
The four pillars of object oriented programming are:
- Inheritance: child classes inherit data and behaviors from parent class
- Encapsulation: containing information in an object, exposing only selected information
- Abstraction: only exposing high level public methods for accessing an object
- Polymorphism: many methods can do the same task
Applications of Object-Oriented Programming
- Client-Server Systems-Object-oriented client-server systems provide the IT infrastructure, creating Object-Oriented Client-Server Internet (OCSI) applications.
2. Object-Oriented Databases- These databases store objects instead of data, such as real numbers and integers.
3. Office Automation Systems-These include formal as well as informal electronic systems primarily concerned with information sharing and communication to and from people inside and outside the organization. Some examples are:
- Word processing
- Web calendars
- Desktop publishing
4.CIM/CAD/CAM Systems
OOP can also be used in manufacturing and design applications, as it allows people to reduce the effort involved.
Advantages of OOP
- Security - Using encapsulation and abstraction, complex code is hidden, software maintenance is easier
- The modular classes are often reusable -Once the modular classes have been created, they can often be used again in other applications or projects
- The coding is easier to maintain -It is easier to create a maintainable procedure code.
- Easily upgradable and scalable -Programmers can implement system functionalities independently.
Disadvantages of OOP
1 .It can be inefficient
2.It can be too scalable.
3.It can cause duplication.
Comparison based on the key criteria
Conclusion
Functional programming and OOP are two of the most popular programming paradigms. Despite having different approaches, both were designed to help developers create efficient and top-quality applications. On the one hand, in OOP, data is stored in objects since the data and respective behaviors (meaning, what a program can do to or with data) should belong to a single location. On the other hand, in functional programming, data is passed and collected by functions. Both programming paradigms have advantages and disadvantages, which is why many developers prefer to design hybrid solutions based on the needs and goals of each project.