OOP vs Functional vs Procedural Programming

date
Jun 29, 2024
slug
oop-fp-prodedural-diff
status
Published
tags
Concept
OOP
FP
Declarative
Imperative
summary
type
Post
Programming languages are tools for solving particular problems. Programming paradigms are a set of rules that prescribes an approach to thinking about the problem.
Programming languages support a set of paradigms that one can use to implement algorithms.

Procedural Programming

The basic construct of procedural programming is blocks of code known as procedures or routines. Procedural programming is all about the idea of getting things done in a sequence of steps.
Unlike OOP, where data and methods are tied together (encapsulated) in a class or object, procedural programming uses data and methods as two different entities.
As a result, there is no concept of access specifiers here, making this paradigm less secure than OOP because of visibility of data across the whole program.

OOP

The main building block of OOP are objects that represent a mental model of an actual object in the real world.
An object has an internal state of attributes (or properties) and a group of associated actions (or behavior) that implements a mental model of an object in the real world.

OOP Principles

Abstraction

The process of hiding the implementation details and showing what is only necessary to the outside world.
This is done with the help of various access specifiers that specify the visibility of each class attribute.

Encapsulation

Wrapping methods and the related fields together as a single unit.

Inheritance

A subclass is the class that inherits the properties and behaviors of superclass. The subclass can then access to methods and attributes of a superclass and add more methods and attributes.

Polymorphism

It allows the same object to behave differently depending on the context.
  • Runtime polymorphism (Overriding) - allows a function to override the previous implementation of a method inside the superclass
    • Compile-time polymorphism (Overloading) - allows a function to have multiple implementations based on function signature

      Functional Programming

      The basic units of functional programming are functions.
      A function can be defined and invoked in any context just as much as a variable can.
      The idea is to remove the difference between functions and data. Everything in your code, therefore, happens through functions and parameters.
      The principles of functional programming are centered around the idea of pure functions (in contrast to impure functions).
      It promotes the use of functions that are transparent, reusable, and modular - therefore allow you to write more efficient code.

      FP Concepts

      Pure Function

      A pure function is one that returns the same output for a given set of inputs, without having any side effects.
      • Have no side effect
        • pure functions will never modify the input arguments they receive or the global state of the program.
          A function can only be called pure if it does nothing other than calculating the value to be returned.
      • Referential transparency
        • The invocation of a function (a function call) would very well be replaced by the value it returns, without affecting anything in your code.
      • Only use the parameters they are passed

      Immutable Data

      Ideally only use immutable data.
      This means that every time you want to do an operation on a variable, you store the updated value in a new variable instead of modifying the initial one.

      Avoiding Shared State

      A shared state refers to variables and objects that exists in a shared scope. This means that one variable can be updated from multiple places.
      Restrict variables and objects to their own scope makes managing and debugging code much easier.

      First-class and Higher-order Functions

      A first-class function is one that can be used just like any other variable.
      A higher-order function is one that receives another function as an argument or that returns a new function or both.
      First-Order Function
      Higher-Order Function
      Function is treated as a variable that can be assigned to any other variable or passed as an argument or returned by another function.
      A function that receives another function as an argument or returns a new function or both.
      The presence of the First-class function implies the presence of a higher-order function.
      The presence of a Higher-order function does not imply the presence of a First-order function.

      Lazy evaluation

      Functional programming allows for lazy evaluation, i.e. values of variables are calculated only when it is required.

      Functional vs. Object-Oriented vs. Procedural Programming

      Functional Programming
      Object Oriented Programming
      Procedural Programming
      Data and functions are the same things; the main focus is on function composition.
      Objects are composed of methods and attributes; the main focus is communication among objects.
      Data and functions are not the same; the focus is on procedures (operations).
      Ideal for concurrent and parallel programming
      Ideal for scalable software systems
      Ideal for general-purpose programming
      For pure functional programming, data are immutable.
      Data and methods can be hidden.
      Data is exposed, and it can be changed in any context.
      Declarative style: Developer describes what your objectives are in code
      Imperative style: Developer specifies how to reach the goal in code.
      Imperative style: Developer specifies how to reach the goal in code.
      Code is organized into functions and modules.
      Code is organized into objects.
      Code is organized into modules and procedures.

      References

      Additional Resources


      © Foy Wan 2022 - 2025