DEV Community

Cover image for Functional Programming vs Object-Oriented Programming: a Beginner Perspective
Patrick Henry
Patrick Henry

Posted on

Functional Programming vs Object-Oriented Programming: a Beginner Perspective

Functional programming and object-oriented programming (OOP) represent two approaches in the world of software development, each with its own way of solving common coding problems. I think that it is really up to the developer to decide whether or not one approach is greater than another.

var hometowns = students.reduce(function(acc, curr){
  if (!acc.includes(curr.hometown)) {
      acc.push(curr.hometown)
      }
  return acc
}, []).join(' ')

function age19Count(array) {
  let num = 0
    function recurse(array){
      if (!array.length){
        return num
      } else if (array[0].age === 19){
        num += 1
      }
      //recursion
      return recurse(array.slice(1))

    }
  return recurse(array)
}
Enter fullscreen mode Exit fullscreen mode

These are two examples I am working on right now that are great examples of functional programming where making first-class functions that are then used to transform pieces of data into new structures. It allows us to parse the data that we actually need and what we don't need.

Functional programming functions as first-class functions and is used to solve all the problems in the code. Functions are treated as reusable pieces of data, allowing for a more understandable coding style. Key features include immutability and the avoidance of side effects. This makes for a lot of predictability in your code and is easy to test. I find it a better approach for beginners because of its ease of understanding and like I said earlier the predictability of what is coming out of a function. It also is a great practice to not be able to hard-code systems. I might also feel this way because I have not worked with OOP systems yet.

//Object of one individual dog
var rufus = {
    name: "Rufus",
    birthday: "2/1/2017",
    age: function() {
        return Date.now() - this.birthday;
    },
    attendance: 0
}

//Object of second individual dog
var fluffy = {
    name: "Fluffy",
    birthday: "1/12/2019",
    age: function() {
        return Date.now() - this.birthday;
    },
    attendance: 0
}
Enter fullscreen mode Exit fullscreen mode

Example via Educative.io
The above example shows how object-oriented programming is implemented in real-life. we can create many objects with the same information very easily by executing the new Obj()
class allowing for ease of use.

Object-oriented programming revolves around the idea of objects, bucketing data and behavior into units. Objects interact with each other through defined interfaces(this), fostering reusable code. OOP emphasizes the principle of inheritance providing a structured way to model modular systems. In preparation for this essay, I looked at a lot of OOP in real-world situations and I found myself struggling to understand and having to jump all over the page to define different parts of the program. While this is the case for functional programming it feels as though you have to do more of Functional programming often fairs in terms of maintenance better due to its focus on unchangeable variables. This can lead to more predictable code and easier debugging. However, OOP shines in scenarios where modeling real-world entities and their relationships is crucial, as it provides a natural way to structure and organize code. I say that it fits better in real-world situations because I can think of a lot of cases where I'd rather use object-oriented programming to write some systems like initializing objects with new user info and other systems, while I'd rather use functional programming to maintain functions in the background and be able to keep track of multiple practices

In conclusion, the choice between Functional programming and OOP depends on the nature of the project and the scenarios demanding simplicity, predictability, and parallelism, while object-oriented programming provides an effective means of modeling complex systems with real-world relationships. Both ways of programming have their pros and cons, and the decision rests on the demands and goals of the software development project that you intend to build. I think I will need a lot more practice with both approaches as I am still a beginner. Maybe I'll look back on this essay and realize how silly I am being. But, I am ready to learn more.

Top comments (0)