DEV Community

Cover image for Functional programming vs object oriented programming
Your Tech Sis
Your Tech Sis

Posted on

Functional programming vs object oriented programming

Functional programming is the programming technique that accentuates the functional factors required for creating and implementing the programs. Simply put, Functional programming (FP) is the process of building software by composing pure functions. Object-oriented programming is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields, and code, in the form of procedures.

Functional programming:
Functional programming is a declarative programming paradigm where programs are created by applying sequential functions rather than statements. Each function takes in an input value and returns a consistent output value without altering or being affected by the program state.
Functional programming is gaining popularity due to its efficiency and scalability to solve modern problems. Functional programming supports programming languages like Lisp, Clojure, Wolfram, Erlang, Haskell, F#, R, and other prominent languages. Functional programming is great for data science work.

Object-oriented programming:
Object Oriented programming (OOP) is a programming paradigm that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. Object-oriented programming languages include JavaScript, C++, Java, and Python. Object-oriented programming is about creating objects that contain both data and functions. A class is an abstract blueprint used to create more specific, concrete objects. Classes define what attributes an instance of this type will have, like color, but not the value of those attributes for a specific object.Classes can also contain functions, called methods available only to objects of that type. These functions are defined within the class and perform some action helpful to that specific type of object.

Functional programming vs Object oriented programming

  • Functional programming emphasizes on evaluation of functions while object oriented programming is based on the concept of objects.
  • Functional programming uses immutable data while object oriented programming uses the mutable data.
  • Functional programming follows the declarative programming model while object oriented programming follows the imperative programming model.
  • Functional programming supports parallel programming while object oriented programming does not.
  • In functional programming, statements can be executed in any order. In OOP, statements are executed in a particular order.
  • In functional programming, recursion is used for iterative data while in OOP, loops are used for iterative data.
  • Variables and functions are the basic elements of functional programming. Objects and models are the basic elements of object oriented programming.
  • Functional programming is used only when there are few things with more operations. Object-oriented programming is used when there are many things with few operations.
  • In functional programming, a state does not exist. In object-oriented programming, the state exists.
  • In functional programming, a function is the primary manipulation unit. In object-oriented, an object is the primary manipulation unit.

  • Functional programming provides high performance in processing large data for applications. Object-oriented programming is not good for big data processing.

  • Functional programming does not support conditional statements. In Object-oriented programming, conditional statements can be used like if-else statements and switch statement.

Which is better?

Well,it depends on what your program is trying to do.
Both OOP and FP have the shared goal of creating understandable, flexible programs that are free of bugs. But they have two different approaches for how to best create those programs.
In all programs, there are two primary components: the data (the stuff a program knows) and the behaviors (the stuff a program can do to/with that data). OOP says that bringing together data and its associated behavior in a single location (called an “object”) makes it easier to understand how a program works. Functional programming says that data and behavior are distinctively different things and should be kept separate for clarity.
In functional programming, data cannot be stored in objects, and it can only be transformed by creating functions. In object-oriented programming, data is stored in objects. Object-oriented programming is widely used by programmers and successful also.

In Object-oriented programming, it is quite hard to maintain objects while increasing the levels of inheritance. In functional programming, it requires a new object to execute functions, and it takes a lot of memory for executing the applications.
Each has their own advantages and disadvantages, it is up to the programmers or developers to choose the programming language concept that makes their development productive and easy.

Top comments (1)

Collapse
 
mittyesque profile image
James Thurber

The "Which is better" section is really helpful.