跳过导航
0

Functional programming is a hot topic amongst developers nowadays, and not without reason. Version 4.5 of WPML was a large-scale project which introduced the option of automatically translating your website. For us, it was also the right moment to make the switch to functional programming. Here’s what we learned in the process.

What is Functional Programming?

You’ve probably heard that functional programming makes your code more readable and is easier to test. But to fully grasp functional programming, it’s important to understand the related concepts of it.

To start with, functional programming is a programming paradigm where the emphasis is on using functions to construct software and composing functions together to build more complex functions. Functional programming makes the code more declarative in nature. You express what you want rather than having a sequence of statements which update the program state.

It’s important to take note of the functions it uses – specifically pure functions. Pure functions receive data and return data, but don’t have any other side effects. This makes the functions very easy to test because if you give the same parameters, you get the same result. 

Simply put, in functional programming the output depends on the input.

Why Functional Programming?

WPML is a large and complex project. Up until recently, we’ve used object-oriented programming (OOP) to try to manage this complexity. One of the main challenges is writing test code (Unit tests) for all of our projects. When writing testable code, we need to ensure any changes we make don’t break existing code.

Unit testing OOP code can only take you so far. Code dependencies need to be mocked so classes can be tested independently. As a result, the tests end up being large and complex.

The main focus of functional programming is said to be “What to solve”. In contrast, object-oriented programming takes on a “How to solve” approach. With this in mind, it seemed like functional programming could help us overcome some of our challenges.

What We Did

When we talk about functional programming, PHP is not the first language that comes to mind – although it does allow us to introduce a functional style. For example, hooks and filters that allow WordPress to be extended use functions to override WordPress functionality.

Unfortunately, there’s no standard library for using a more functional paradigm in PHP. We reviewed many libraries but none had exactly what we needed to write expressive code in a functional style. So instead, we decided to write our own library. We drew inspiration from different sources, but many from Javascript libraries, which we found were more mature.

Functional programming has a different approach than other paradigms, so switching to a functional programming paradigm is actually a big step. It requires you to change the way you think about writing code and solve problems. It takes a lot of practice to write expressive code that is clear and concise.

Did It Work?

We definitely think it did. Functional programming has a number of features that work to our advantage:

  • The code structure is better 
  •  The code is more expressive and concise
  • Code duplication is reduced
  • Functions are smaller and therefore easier to test and reuse
  • Functions can be composed together
  • Less mocking in tests
  • Reusable mocking libraries were developed

But of course, nothing is perfect. While functional programming is more concise and predictable, it does have disadvantages, especially if you’re unfamiliar with it: 

  • It’s harder to debug
  • It’s harder to change – although this can also be seen as an advantage, because it requires you to consider changes rather than introducing quick changes
  • The learning curve is steep

Don’t Be Afraid To Try

It goes without saying that the popularity of functional programming is more than just a trend. Because of its nature, functional programming is great for tasks such as machine learning and data analysis. As a result, companies of all sizes have started to show interest in, or even begin their transition to functional programming. This doesn’t mean that you should say goodbye to object-oriented forever, but it’s worth giving functional programming a try. Even if you never end up using it, chances are you’ll up your game as a developer. 

If you’re interested in learning more, we suggest starting with the Composing Software blog post series.

Once you’re ready to dive deeper, take a look at our FP project repo.

Do You Live and Breathe Code? Join Us.

We’re always looking for exceptional developers to join our team. If that sounds like you, don’t wait any longer – apply today.