Functional Programming vs Object Oriented Programming

Functional Programming vs Object Oriented Programming
0

#1

I’m wondering if anyone can help me understand the approaches to each approach. Differences, similarities, reasons to use each etc. Any article or response is much appreciated!


#2

Oh my, this are both large topics. Without getting too much into it, it’s hard to contrast them because they’re not really in the same domain. Many object-oriented languages are not strictly functional, and vice versa, but most of the big languages today (as far as I’m aware) have some facility for functional programming. JavaScript in particular is neither purely object-oriented nor functional, but we’re able to write code in a functional and object-oriented style.

There’s already a large thread on object-oriented programming, so I won’t get into it, but I do prefer writing my code with functional programming in mind, so I’ll leave a few words here on that. From the Wikipedia entry (emphasis mine):

In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) each time. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.

source

One of the best things to take from functional programming, IMHO, is to not rely on side effects to change data. For instance, look at this pile of rubbish:

function add(a, b) {
  sum = a + b;
}
var sum;
add(4, 5)
console.log(sum) // 9

This may seem harmless, but there are some serious problems with this code. What happens if some other part of the program is depending on sum? Maybe changing sum is the size of a display window, or the amount of money being deposited in a bank account, or the thrust of a rocket engine. Whatever it is, it’s liable to change unexpectedly because sum will change each and every time that add() is called. This takes your code from being a sleek and efficient software machine to being a state-changing circus where the acrobats are drunk clowns.

What’s better is to write your code such that your functions produce an output. If you don’t capture that output, nothing changes. You don’t lose the data you had, and you don’t change data anywhere else in your program. Leave the acrobatics to the professionals, and lock the clowns out of the liquor cabinet.

function add(a, b) {
  return a + b;
}
var sum = add(4, 5);
console.log(sum); // 9
add(1234,5678); // Produces the number 6912, which is sent harmlessly into the void

The functions that you most often see associated with functional programming, especially in JavaScript, are array methods like map(), filter(), and reduce(). These follow the same idea, allowing you to use one array to create another without changing the first array or anything else in your code. Compare and contrast:

function drunkClownChaos(arr) {
  for(var i = 0; i < arr.length; i++) {
    newArr.push(arr[i] * 2);
  }
}
var newArr = [];
drunkClownChaos([0,1,2,3]);
drunkClownChaos([9,5,3,2]);
console.log(newArr); // [0,2,4,6,19,10,6,4] -- oh my, I only wanted four numbers in the array!

var newArr = [0,1,2,3].map(function(x) {
    return x * 2;
});

newArr = [9,5,3,2].map(function(x) {
    return x * 2;
});

console.log(newArr); // [18,10,6,4]

Depending on where you’re at in your coding journey, this could all make a lot of sense, no sense at all, or I could look like fool.


#3

Thanks @PortableStick! This definitely cleared a lot of things for me.

On another note, I feel throughout my coding journey I experience things making sense, no sense, and looking like a fool ALL THE TIME. Sometimes all at once… lol. :joy: