Although the most common development paradigm in modern development environments is object-oriented programming (OOP), developers would benefit from taking a closer look at functional programming, said Uptime’s .NET developer/architect Tanel. Although the functional approach doesn’t fit every solution, knowledge of its core principles helps to write cleaner and more effective code within any paradigm.
“Functional programming means that we approach code through process. While in OOP we define objects – there is a car and it does certain things – then with functional programming, we describe the process – driving, what is needed for the activity, and what will happen as a result,” Tanel explains. “Philosophically it is a rather different approach and requires some practice, but nevertheless it’s worth the time investment.”
The possibilities offered by functional programming have several advantages when compared to OOP but lag behind in other areas. Hiob said that generally, the functional approach results in code that is considerably shorter and can be written more quickly; also, the final product is less buggy, because the paradigm slaps you for making mistakes even before anything moves forward in the pipeline.
Whilst in functional programming the emphasis is on readability and in a more logical structure, it comes at the expense of performance and speed. Therefore, Tanel recommends to prefer a functional approach in case of business applications and other less intensive applications and continue to favour OOP for high performance solutions, such as user interfaces.
A perfect compromise
While in the past OOP and functional programming were treated as two separate approaches, more and more signs can be seen of one affecting the other. One can see approaches stemming from functional programming in the work of many developers already today, such as the use of constant variables, lambdas, and pure functions, Tanel noted.
Also, developers can use both OOP and functional programming paradigms in the same project if they wish. Tanel observed that the functional approach is easier to apply when building solutions from scratch; however, there would be no problem in building a whole service based only on the OOP principles and employing the functional approach for a subservice – especially easily performed with .NET where C# and F# can be used side by side fluently. The existing applications can benefit from adding more and more fragments of functional programming during follow-up development.
“It is worth remembering that although several OOP languages are slowly starting to apply the possibilities offered by functional programming, the languages created specifically for that approach, such as F#, Clojure or Haskell encompass a range of options not found elsewhere,” he explained. “For instance, F# can get tables or data from a database already when writing the code and can provide autocomplete. Option types, partial application of functions and dependency injection during compilation are also powerful advantages.
Better readability, less potential bugs, better development velocity and improved code quality are the main arguments in favour of functional programming alongside the convenience functions.
Both paradigms have their merits
Modern development environments are ruled by OOP and most probably this will continue for some time, Tanel said. However, this does not mean that developers should not attempt to become familiar with the functional approach and if possible, make use of it in some of their projects.
“Junior developers could definitely start with learning OOP, finding a job, putting their knowledge into practice and then tackle the foundations of functional programming,” he advised. “Experienced developers could get to know its principles and try and put these into use here and there, until a possibility opens up to create a larger piece exclusively based on the principles of functional programming.”
Tanel highlighted that even if one cannot use the functional programming paradigm in their everyday work, keeping those principles in mind will help to create better OOP-based applications as well. “It makes you think differently and pay some attention to different details.”