Sobes.tech
Junior — Middle

How to distinguish between object-oriented and functional approaches in programming?

sobes.tech AI

Answer from AI

Object-Oriented Programming (OOP) and functional approaches differ in their core principles:

  • OOP is built around objects — entities that combine data and methods to operate on them. Key concepts include classes, inheritance, encapsulation, and polymorphism.

  • Functional programming focuses on pure functions that do not modify state and have no side effects. It emphasizes higher-order functions, immutability, and recursion.

Example in Python:

# OOP
class Counter:
    def __init__(self):
        self.count = 0
    def increment(self):
        self.count += 1

c = Counter()
c.increment()
print(c.count)  # 1

# Functional style
from functools import reduce
numbers = [1, 2, 3, 4]
sum_numbers = reduce(lambda x, y: x + y, numbers)
print(sum_numbers)  # 10

In OOP, state is stored in objects, whereas in the functional approach, data is immutable, and computations are expressed through functions.