The Power of Mixing Functional Programming and Object-Oriented Programming

G7vo...Hj1n
22 Jul 2023
75

I have been at both ends of development and honestly best paradigm is optimally using best of both depending on the scenario.

Functional Programming 

Functional programming emphasizes composing functions to build complex systems from simpler building blocks. Languages like Lisp, Erlang, Haskell, F#, Elm are perfect example of it. 
It provides advantages like efficiency, lazy evaluation, nested functions, bug-free code, and parallel programming.

OOP(Object-Oriented Programming)

As the name implies, Object-Oriented Programming is all about using entities called “objects,” which pretty much governs the whole framework of this programming language. Languages like Java, Python, Ruby, PHP, Perl, Objective-C, Swift govern the foundation of OOP.

Procedural programming?

Procedural programming is a programming paradigm that focuses on writing procedures or routines that perform specific tasks. It uses a top-down approach, where the main program is divided into smaller procedures or functions, each responsible for a specific task. 
Some languages like Go, C, COBOL, Pascal are foundation of procedural or you can as well say imperative language.

Comparison

As Python can be used as both in OOP and FP paradigm will use it as example:

class Animal:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def speak(self):
        pass

    def display_info(self):
        print(f"Name: {self.name}, Age: {self.age}")

class Dog(Animal):
    def __init__(self, name, age, breed):
        super().__init__(name, age)
        self.breed = breed

    def speak(self):
        return "Woof!"

    def display_info(self):
        print(f"Name: {self.name}, Age: {self.age}, Breed: {self.breed}")

class Cat(Animal):
    def __init__(self, name, age, color):
        super().__init__(name, age)
        self.color = color

    def speak(self):
        return "Meow!"

    def display_info(self):
        print(f"Name: {self.name}, Age: {self.age}, Color: {self.color}")


# Create instances of the classes
animal = Animal("Generic Animal", 5)
dog = Dog("Buddy", 3, "Labrador")
cat = Cat("Whiskers", 2, "Gray")

# Display information and sounds for each animal
animal.display_info()  # Output: Name: Generic Animal, Age: 5
print(animal.speak())  # Output: None

dog.display_info()  # Output: Name: Buddy, Age: 3, Breed: Labrador
print(dog.speak())  # Output: Woof!

cat.display_info()  # Output: Name: Whiskers, Age: 2, Color: Gray
print(cat.speak())  # Output: Meow!pp


A lot of people start to dislike OOP but it has it’s benefits and why is it still widely adopted.

  • Software with complex relationships between entities (e.g., games, simulations, enterprise applications).
  • Systems requiring extensive code reuse through inheritance and polymorphism.
  • GUI-based applications where encapsulation helps manage UI elements and their interactions.
# Higher-order function to create an Animal
def create_animal(name, age):
    return {"name": name, "age": age}

# Function to make an Animal speak
def animal_speak(animal):
    return None

# Higher-order function to create a Dog
def create_dog(name, age, breed):
    return {"name": name, "age": age, "breed": breed}

# Function to make a Dog bark
def dog_speak(dog):
    return "Woof!"

# Higher-order function to create a Cat
def create_cat(name, age, color):
    return {"name": name, "age": age, "color": color}

# Function to make a Cat meow
def cat_speak(cat):
    return "Meow!"

# Function to display information for any animal
def display_info(animal):
    print(f"Name: {animal['name']}, Age: {animal['age']}")
    if 'breed' in animal:
        print(f"Breed: {animal['breed']}")
    if 'color' in animal:
        print(f"Color: {animal['color']}")

# Example usage
animal = create_animal("Generic Animal", 5)
dog = create_dog("Buddy", 3, "Labrador")
cat = create_cat("Whiskers", 2, "Gray")

display_info(animal)  # Output: Name: Generic Animal, Age: 5
print(animal_speak(animal))  # Output: None

display_info(dog)  # Output: Name: Buddy, Age: 3, Breed: Labrador
print(dog_speak(dog))  # Output: Woof!

display_info(cat)  # Output: Name: Whiskers, Age: 2, Color: Gray
print(cat_speak(cat))  # Output: Meow!

Overall, functional programming’s strengths in handling parallelism, data processing, modularity, and readability have made it attractive to developers, especially as software systems grow in complexity and scale.
In both examples personally I would prefer OOP approach same when handling state , focus on design patterns etc. Functional on the other hand building AI models if there is concurrency in code, data transformation etc.

# Using Object-Oriented Programming (OOP)
class NumberProcessor:
    def __init__(self, numbers):
        self.numbers = numbers

    def filter_even_numbers(self):
        self.numbers = [x for x in self.numbers if x % 2 != 0]

    def double_numbers(self):
        self.numbers = [x * 2 for x in self.numbers]

    def calculate_sum(self):
        return sum(self.numbers)

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

processor = NumberProcessor(numbers)
processor.filter_even_numbers()
processor.double_numbers()
result = processor.calculate_sum()

print(result)  # Output: 50


# Using Functional Programming (FP)
from functools import reduce

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Filtering out even numbers
filtered_numbers = filter(lambda x: x % 2 != 0, numbers)

# Doubling each remaining number
doubled_numbers = map(lambda x: x * 2, filtered_numbers)

# Finding the sum of the resulting values
result = reduce(lambda x, y: x + y, doubled_numbers)

print(result)  # Output: 50


At the above given examples FP is a clear winner in readability being concrete less LOC. The OOP approach involves creating a class NumberProcessor with methods for each transformation step. While it's certainly possible to use OOP for data transformations, in this particular example, FP provides a more concise and straightforward solution.

Concluding
There is a huge preference over a paradigm that one is better then the other but honestly I think it depends on the scenario. When I see OOP heavy code in Python I sometimes feel it’s overcomplicated but then again can be quite flexible in other cases and reduce code duplications and expand on created instance. 
I use more procedural language like as of late Go what is really pleasing but there are duplicate code often as there is no inheritance and can’t pass state same as fully functional language.
Carefully consider the unique needs of your projects and sometimes use a hybrid approach, combining the strengths of both paradigms to achieve the best results.


Get fast shipping, movies & more with Amazon Prime

Start free trial

Enjoy this blog? Subscribe to Mozes721

6 Comments