Functional vs. Object Oriented Programming

I have recently started learning about functional programming and it seems to be a very powerful tool.

During the learning process, I have tried to do a quick comparison of functional programming and object-oriented programming. The goal here was to understand the difference in how we think while programming in these 2 domains.

I have selected the Map functionality for doing the comparison. Mapping is basically applying a function to all elements of a list, and return the resulting list. More details can be found at https://en.wikipedia.org/wiki/Map_(higher-order_function).

I have first implemented Map functionally (this might get confusing as Map is a classic primitive in functional programming, but I am not using any built-in language functions here):


func functionalMap(inputArray: [Int], theFunction: Int -> Int) -> [Int] {
    
    var resultArray: [Int] = []
    
    for integer in inputArray {
        
        resultArray.append(theFunction(integer))
    }
    
    return resultArray
}

// Usage
var funcedArray = functionalMap(myArray) { x in x + 5 }

For sake of simplicity, I have implemented Map only for Integers. This could be easily extended to support any type.

Then, I have implemented it in the Object-Oriented way. There is more than one way to skin a cat, so I am sure this could have been done in different ways. Here is my implementation:


protocol IntElementMutator {
    func mutateIntElement(inputElement: Int) -> Int
}

func objectOrientedMap(inputArray: [Int], mutator: IntElementMutator) -> [Int] {
    
    var resultArray: [Int] = []
    
    for integer in inputArray {
        
        resultArray.append(mutator.mutateIntElement(integer))
    }
    
    return resultArray
}

struct AddFiveMutator : IntElementMutator {
    func mutateIntElement(inputElement: Int) -> Int {
        return inputElement + 5
    }
}

let fiveMutator = AddFiveMutator()
var mutatedArray = objectOrientedMap(myArray, mutator: fiveMutator)

With these two implementations in hand, the following conclusions can be made:

- Functional way is a lot more concise and expressive
- Stateless nature of functional programming is definitely a relief
- Object Oriented way offers a more structured approach, making it just a bit harder to make mistakes. This of course comes with a cost; it is way too verbose.

The example was way too simple; so it is hard to draw sophisticated conclusions on functional and object-oriented programming via this example. The impression that I have got is functional programmer is a bit more powerful and with more power comes greater responsibility. You have to be  more careful using it since it is easier to make mistakes.

As usual, it must be mentioned that neither functional programming nor object-oriented programming is the answer to all your programming needs. It is great to have both skills and it is even greater to know when to apply which. That is the ultimate power!

Cheers,
Guven.

(Both of the implementations can be found at: https://github.com/5fcgdaeb/functionalswift)


Comments

Popular Posts