76techs / Blog

Functional vs Object-Oriented Programming (OOP)

|

When to use FP, OOP, or a mix of both in real projects.

Functional vs Object-Oriented Programming (OOP)

Two of the most common programming paradigms are Object-Oriented Programming (OOP) and Functional Programming (FP). A paradigm is a style of thinking about how to structure code and solve problems. Most real-world projects mix both.

Object-Oriented Programming (OOP)

What it is

OOP organizes code around objects, which bundle data (state/properties) and behavior (methods). You model your system as things such as User, Order, Invoice, and PaymentService, and each object is responsible for part of the behavior.

Core ideas

Encapsulation: hide internal details and expose a clean API

Inheritance: reuse behavior by extending classes (can be overused)

Polymorphism: different objects can be treated similarly via an interface or contract

Advantages of OOP

Great for modeling domains that naturally look like entities with state

Encapsulation helps maintainability by controlling state changes

Reuse through composition/inheritance can reduce duplication

Works well with many mainstream ecosystems and frameworks

Disadvantages of OOP

Shared mutable state can create bugs that are hard to trace

Inheritance can create tight coupling and fragile hierarchies

Can encourage over-engineering with too many classes too early

Testing can get harder when objects depend on many other objects

Functional Programming (FP)

What it is

FP organizes code around functions and favors immutability, pure functions, and composition. Instead of objects that do things, FP often treats data as values and functions as transformations.

Common FP ideas

Pure functions

Immutability

Higher-order functions

Function composition

Declarative style

Advantages of FP

Easier reasoning because pure functions are predictable

Fewer side-effect bugs due to less hidden mutation

Great for concurrency and parallelism

Often easier to test because pure functions need less mocking

Disadvantages of FP

Can feel less intuitive at first if you learned OOP first

Immutable updates can create extra allocations

Some domains can feel awkward without objects

FP-heavy abstractions can become hard to read if overused

Quick comparison

OOP is usually strong when:

You have many entities with state and well-defined behavior

You want encapsulation and clear boundaries

You work in ecosystems built around classes and objects

FP is usually strong when:

You want predictable logic and easier testing

You are transforming data pipelines (map/filter/reduce)

You want to avoid bugs from state mutation

You do concurrent work where shared state is risky

Real-world truth: most apps mix both

In many web apps, FP and OOP coexist and complement each other:

FP style for data transforms, validation rules, reducers, and formatting

OOP style for services, domain models, and modules with lifecycle and internal state

Side-by-side example (JavaScript)

Example 1 — Functional style (FP)

const cart = [

{ name: "Keyboard", price: 100, qty: 1 },

{ name: "Mouse", price: 50, qty: 2 },

];

const subtotal = (items) => items.reduce((sum, item) => sum + item.price * item.qty, 0);

const applyDiscount = (amount, discountRate) => amount * (1 - discountRate);

const applyTax = (amount, taxRate) => amount * (1 + taxRate);

const applyCoupon = (amount, couponCode) => {

const coupons = { SAVE10: 0.10, SAVE20: 0.20 };

const rate = coupons[couponCode] ?? 0;

return applyDiscount(amount, rate);

};

const total = (items, { discountRate = 0, taxRate = 0, coupon = null } = {}) => {

let amount = subtotal(items);

amount = applyDiscount(amount, discountRate);

if (coupon) amount = applyCoupon(amount, coupon);

amount = applyTax(amount, taxRate);

return amount;

};

Why this is FP

Predictable functions: same input, same output

No hidden mutable state

Easy to test each function in isolation

Example 2 — Object-Oriented style (OOP)

class ShoppingCart {

constructor(items = []) {

this.items = items;

this.discountRate = 0;

this.taxRate = 0;

this.coupon = null;

}

setDiscountRate(rate) { this.discountRate = rate; return this; }

setTaxRate(rate) { this.taxRate = rate; return this; }

setCoupon(code) { this.coupon = code; return this; }

subtotal() {

return this.items.reduce((sum, item) => sum + item.price * item.qty, 0);

}

applyCoupon(amount) {

const coupons = { SAVE10: 0.10, SAVE20: 0.20 };

const rate = coupons[this.coupon] ?? 0;

return amount * (1 - rate);

}

total() {

let amount = this.subtotal();

amount = amount * (1 - this.discountRate);

if (this.coupon) amount = this.applyCoupon(amount);

amount = amount * (1 + this.taxRate);

return amount;

}

}

Why this is OOP

Keeps internal state (items, tax rate, coupon)

Encapsulates logic in a class

The cart object knows how to calculate its total

Which style is better here?

FP is great when you want predictable and highly testable functions. OOP is great when you need a stateful cart object with configurable rules and lifecycle. In production apps, mixing both is often the best strategy.