Functional Programming vs Object Orientated Programming Examples?

I know I read this already Functional versus Object-Oriented: help me understand the purpose of it all

My only problem with it is that there are no actual sample code to do any comparisons, I also learn better by reading code and I also wanted to make a Poll

Which do you prefer?

  • Functional Programming
  • Object Oriented Programing

0 voters

while I am asking what you prefer, keep in mind that there are no right or wrong answers it’s preference, however both Programming paradigms have there own purpose for different things, which is what I’d like to find out.

I would use both of them or any other paradigm when it’s the best option for what I want to achieve.

There’s also a thing called Data Oriented Programming and Entity component system, what are those?

If you’d be kind and provide sample both for FP and OOP for us to compare, thanks!

7 Likes

For pure functional programming, look no further than Haskell. For scuffed object oriented programming, throw a rock at Java. If you want some Lua-related sampling of what it might look like to use either paradigm, it’s a bit hard. There are a million ways to implement either. But when I think of code relating to them, I think:

-- sort an array functional-y
local array = Array {6, 2, 49, 4, 6}
local sorted = array:iterable():sort(function(a, b) return a < b end):collect()
-- sort an array oop-y
local array = Array.new {6, 2, 49, 4, 6}

array:sort(function(a, b) return a < b end)

Which kind of suck as examples, but what I mean to illustrate is that in FP you have immutable state with no side effects, and in OOP you have mutable state you act upon.

If I’m not mistaken, OOP is similar to modules in a way? If you’re using modules alot, possible chances you’re doing something similar with OOP though I may be wrong.

Using modules doesn’t necessarily mean you are using OOP. Modules are meant to allow us to write reusable libraries of code we can use in different scripts.

2 Likes

Wow, that looks like I’d hate FP so much because of how much extra code you have to write, not to mention that it looks more complicated than it’s suppose to.

However I appreciate your response very much, thank you for the explanation!

That’s not always the case. Functional programming and OOP has both their strength and weakness, it depends on what you’re trying to achieve. If it’s a small task, there no need to use OOP at all it would be considered a bit overkill. OOP is great for its abstractness. For instance, creating your own weapon system you would want the basic skeleton of the weapon class which would consider the entity class. The entity class would have all the common traits that all weapons would have; such as damaging, equip, unequip and etc. So if you want to create a specific type of weapon you can just inherit all the methods from the weapon’s entity class.

There’s actually something called “The 4 pillars of OOP”, which are Encapsulation, Inheritance, Abstraction, and polymorphism that make up the paradigm. So if you’re thinking of diving into OOP, you should learn each one at a time.

1 Like

Thank you for your response.

I am no stranger to OOP, in fact I over complicate things by using OOP, probably not the right way however I know the concept and those 4 pillars.

What I am not familiar with is Functional Programming, like what is the difference?

Is everything is incapsulated for OOP and everything is individual and independent for Functional Programming?

Object:Function()

vs

Function(Object)

?

1 Like