DEV Community

Yordi Verkroost
Yordi Verkroost

Posted on

The Journey Into Functional Programming

We live in a world dominated by object-oriented programming languages. It's sometimes easy to forget that the world is not flat, that there's more on the horizon. More than the OOP paradigms you're used too.

Or at least, that's the case for me. I have touched upon "the other side of programming", but that was in university. There, I worked with functional programming languages like Haskell, OCaml, and Lisk. Quite different compared to object-oriented programming, but it had its advantages.

But you know how it goes. When landing a job as a software developer, you start using object-oriented languages. That was my case exactly. I rolled into languages like C# and Java and forgot about that other world. Like New Zealand, you've heard of it and seen it in pictures. But it's also a place you've never visited and never will.

Until recently.

A friend of mine mentioned that his team uses a functional language. And to be honest, I'm hearing about functional languages more and more these days. Not only by high school students and academics, but also in business environments. So I asked myself: "How about diving into one of those functional languages again?" To see what the hype is about. It can't hurt, right?

To learn functional programming paradigms, you need a language to practice with. To keep it simple, I chose the language that my friend also uses. It's always a plus if you have someone you know who you can ask questions to whenever you get stuck.

That language is Elixir.

For me, the best way to learn a new programming language roughly follows this path:

  1. Get to know the basics of the language by skimming through the documentation. You can find Elixir's documentation here.
  2. Following a (video) course. A paid one, because that will make you regret it when you drop out. I chose The Complete Elixir and Phoenix Bootcamp on Udemy.
  3. Experiment with the language on your own. I want to do this after finishing the Udemy-course and already have some ideas I could pursue

The thing I wanted to know was how "functional" differs from "object-oriented". After a quick search on the internet, I learned that most sources explain it as follows:

In Object-oriented programming you write how to do something, while in functional programming you write what to do. In other words: object-oriented programming requires you to write code step-by-step to reach the goal. Functional programming requires you to tell the compiler what functions to execute to reach the goal.

Another way of looking at the difference is to have a closer inspection of the naming of both paradigms. Object-oriented programming lets you declare and mutate objects that map the real world. Functional programming lets you declare functions that have an input and an output. But those functions know nothing about an outside world and the context in which they're used. That means that functional programming, functions are pure. The same input will always give you the same output.

Yet another way to compare is on state. Object-oriented programming is stateful, while functional programming is stateless.

Let's look at an example to show these differences between object-oriented and functional. The following two pieces of (pseudo-)code define a student and then print out his name.

Object-oriented

class Student 
{
  string Name;
  int Age;

  Student(string name, int age)
  {
    Name = name;
    Age = age;
  }

  PrintName()
  {
    Console.WriteLine(name);
  }
}

var student = new Student("Bob");
student.PrintName();
Enter fullscreen mode Exit fullscreen mode

Functional

module Student
{
  print_name(student)
  {
     IO.puts Enum.at(student, 1)
  }
}

student = ["Bob", 19]
Enter fullscreen mode Exit fullscreen mode

The object-oriented code models a Student object to its real-world counterpart. Furthermore, any logic related to a student is part of the Student class.

The PrintName() function could return different results each time it's called. For example, first initialize a new Student who's name is Bob. Then print the name. Afterward, you could change the value of Name and then print the name again. While you call the same function, you will get a different result. This shows that the PrintName() function is not pure.

The result of the print_name() function is always the same for the same input. Here you're not using the internal state of an object to print the name. You're actually giving the whole student array as input to that function.

Also notice that we cannot use an actual object to represent a student. So we use an array that holds the student's name and age. We then get the first element of the array (of which we know it holds the name) and print it. Every time we give the same student as input to the print_name() function, it will return the same output. In other words, this function is pure.

This is a small glimpse of the differences between object-oriented- and functional programming. These are the very first steps in the functional field for me. I hope this article gives you a good first overview as well.

Thanks for reading this post. If you enjoyed it, don't hesitate to give a few claps or leave a comment to start a discussion. I'd like to hear from you!

Top comments (0)