Object-oriented vs Functional Programming

Object-oriented and Functional programming are two popular programming paradigms. Paradigms are sets of patterns we follow when building programs. They can be useful to help avoid bugs and add consistency to our code. Both paradigms have different strengths and should be chosen with intention.

Let’s take a brief look at both and explore what they do.

Object-oriented programming

Object-oriented programming (OOP) is built on classes and objects. Classes are simply templates that create objects. Objects are a data structure that stores data in unordered name-value pairs.

// Create a class 
class Animal {
  constructor(name, age) {
    this.name = name 
    this.age = age
  }
  
  age() {
    return `${this.name} is ${this.age} years old.`
  }
}

// Use the class to create an object 
const dog = new Animal("Frankie", 4)

Objects represent data in its properties. Object methods allow us to perform actions on the object’s data.

In the example above, we have an object Dog that is an instance of the class Animal. The properties name and age represent characteristics of a particular animal, whilst the method age returns a string informing us of the animal’s age.

Key concepts

WhatHowWhy
EncapsulationEnclose methods and attributes within the objectMakes code modular
AbstractionConfine implementation logic from the userCreates separation of concerns, add tidiness
InheritanceCreate new classes by extending existing onesEncourages reusability
PolymorphismAllow objects to take on multiple formsAdds flexibility, avoids code duplication

Functional programming

Functional programming (FP) organises programs and applications into functions. It avoids mutating data and using shared state, relying heavily on the power of pure functions.

Pure functions are functions that will always return the same output given the same arguments. Because they do not mutate data, they do not cause any side effects. Side effects are changes to data in the global scope or any effect produced that is unintended. Pure functions help to create predictability and reliability in our code.

function add(a, b) {
  return a + b
}

console.log(add(4, 2)) // 6

In the example above, add is a pure function. The next time we call it with the arguments 4 and 2 the result will still be 6.

Key concepts

WhatHowWhy
Pure functionsOutputs the same unmodified input every timeOutcomes are predictable, avoids side-effects
RecursionImplements functions that calls itself until a condition is metFills the gap left by for and while loops used in other paradigms
ImmutabilityAvoids modifying data to preserve stateInstills predictability and maintainability

Summary

Object-oriented and Functional Programming are two paradigms that serve different purposes. How you choose a paradigm can depend on your objectives, team experience, or frameworks, libraries and tools you use.

Some programming languages allow you to use both. JavaScript is a multi-paradigm language that supports both paradigms mentioned.

Leave a comment