What is functional programming and how do we use that technology at Nubank?

Find out what is a functional paradigm, how it differs from other paradigms and how we use that technology at Nubank.

Photo of 2 nubankers playing basketball on the court that is on the roof of Nubank's office

If you work with technology or are curious about it, you’ve probably already heard of functional programming. It isn’t anything new, but it’s getting increasingly more attention due to the technological changes we’ve been going through.

What is functional programming?

Functional programming is a programming paradigm, that is, the way of doing something. In programming, it is the methodology used to write codes.

The functional paradigm is based on a very old model of computation called lambda calculus. The lambda calculus was created in 1930 by Alonzo Church (who was a professor of famous mathematician Allan Turing) and it was the foundation for the creation of LISP – a family of programming languages developed by John McCarthy in 1958.

In order to know what functional programming is, however, it is important to understand other famous programming paradigms: imperative programming and object-oriented programming (OOP).

Imperative Programming

The most famous programming paradigm. In imperative languages – such as C and PHP –, codes are written as a list of actions or commands that are run by a computer. They are like orders given to the machine, which is why it is called imperative programming.

Object-Oriented Programming

As for object-oriented programming, a programmer creates data in the form of fields, known as attributes, and codes in the form of procedures, known as methods. One of its characteristics is that the procedure of an object can access or even change the data fields of the object with which it is associated.

As a simple example, imagine that a car is an object and its attributes are its doors, color, etc. You could have a method in which the procedure would be to open the car door and another one to close the car door.

A few examples of object-oriented programming languages are Java, Python, and Ruby. Other languages, such as JavaScript and PHP, support object orientation.

Functional Programming

When we understand other paradigm types, it becomes easier to understand the functional paradigm.

Unlike imperative and object-oriented programming, functional programming considers everything as a function. There isn’t a list of instructions or objects to be run by the computer, but rather a sequence of mathematical functions that together will solve a problem.

That means that in the functional paradigm, functions are pure, do not change the state of the object and give emphasis to expressions and statements, instead of following instructions.

In the example with the car, if we create a pure function to open the car door, that function will not change the car, which is the initial input. You must explicitly use the result of the function, which would be the car with the door open, instead of accessing the initial input, as this is the car with the door closed.

Besides that, there are no variables in the functional paradigm, only constants. This translates into more objective codes with constants that, generally speaking, do not change. A few examples of functional languages are Clojure, Haskell, and Elixir.

What are the benefits of functional programming?

As said before, the code in functional programming tends to be more objective and shorter than in other paradigm types because you are able to isolate the pure functions that will obtain the logic of your business from the functions that are called mutable, which are operations that actually change the object, such as modifying data in a database, for example. 

Another benefit is that, since it is based on mathematical functions, the functional paradigm prompts the use of the concept of immutability. An example of that would be a simple mathematical function, such as f(x) = x + 2. Whenever we use the same value for that function, it will provide an equal, immutable result.

Because of that, code maintenance and eventual changes are easier to apply. It is simpler to add tests and isolate a function in order to make analyses and correct flaws.

It may seem like a small detail, but by having an immutable code, we are sure that when we test it, the code will not act in an unexpected way in a production environment.

In addition to that, functional languages are friendlier to the implementation of parallel computing, meaning that different parts of the system are run smoothly by different processors. That can be explained by the fact that the codes are predictable and immutable, with no side effects.

How do we use functional programming at Nubank?

When Nubank was founded in 2013, we looked for technology that could help us reach our business goals, maintain efficiency and scale up in a safe and sustainable way. 

At that moment, the functional paradigm seemed to be the best option for the challenges we had to face. Because of that, we ended up adopting Clojure as the main language for our services and Datomic as our database.

The principle of immutability of the functional paradigm aligned with Datomic is crucial for a financial institution because, as an append-only database, Datomic keeps an operation history. With that we can, for example, reprocess all Nubank’s data since day one, if necessary, or recompute the operations of a specific day.

The functional paradigm also helps with the process of internationalizing Nubank. The financial system logic of a country may be different from the logic of another country, but it is not necessary to build it from scratch. It is enough to just rewrite the financial system logic and reuse the other parts of the system. 

It is important to say that using a functional language makes things easier, but it is not the only thing necessary to reach that goal. The concept of platformization we use and the C4 model also have an influence on that.

But at the end of the day, it is like playing with Legos: If a piece does not fit, it is easy to exchange it for another one without losing everything that was already built. – Concept of modular software architecture

That way, we can use all the benefits of functional programming to build solutions that give back people’s control over their own money, regardless of the country they are in.

Enter your name