What is the future of software? Some people say that functional programming is the future. Functional programming is a programming paradigm that focuses on the idea that computer programs should be treated as a function that takes input and returns an output. How can that knowledge benefit you and your company?
Why Functional Programming?
In the modern era, functional programming has gained prominence. The reason being in part because of the rise of agile software development, which favors programming methods that improve productivity and software quality. Another explanation is an increased focus on data engineering, reliability, and scalability. This growth in software expectations has fueled the popularity of new programming languages, such as Clojure and Scala. A functional approach offers several benefits, including better code, avoiding spaghetti code, and writing less buggy software. Programs written in functional languages tend to be cleaner and simpler, producing more reliable and easier-to-read software.
What is Functional Programming?
Programming using functional techniques involves applying and composing functions. First-class citizens in functional programming, functions can be bound to names, passed as arguments, and returned from other functions, just like any other data type. Focusing on input and output allows the developer to reason in terms of type transformation. Composing smaller functions into larger ones in a modular manner allows for more readable programs.
Ideally, a functional program should be built only using pure functions. To be pure, a function must exhibit a few properties. The first one is determinability, which means that the function must have a predictable constant output each time it is executed with the same inputs. The second rule is that every input must return an output – no exceptions or nulls are allowed. In addition, the function must not generate any side effects – no database calls, no changes to variables located outside the function scope, and not even print statements – meaning that it shouldn’t affect anything outside the scope of its execution. As a general rule of thumb, you can tell if your function is pure if you can replace it with its result and the program still works identically.
“Because the calls to databases and other outside services are abstracted, in some cases, it is even possible to execute the entire Main of the application as a unit test.”
What are the consequences of embracing FP?
Types can help provide more support to programmers, especially to build resilient software. It is particularly useful in critical applications such as data processing or web APIs. With Functional Programming, the developer can use the compiler as an ally to catch bugs before the application run. This may mean saving hours of computation when a runtime error leads to a crash in data engineering applications. It also allows translating API contracts directly into the types system.
The type system in combination with writing pure functions enables the engineers to write very complete suites of tests because every function can be replaced by its result, are deterministic, and without side effects. The desire to write pure functions also results in the extraction of interactions with other systems – such as databases – into wrappers. Because the calls to databases and other outside services are abstracted, in some cases, it is even possible to execute the entire Main of the application as a unit test. Furthermore, this abstraction of external services allows for an easier replacement of one provider with another by simply modifying the interface implementation.
I could go on and on about the advantages of functional programming but this article will end up being a book. In summary, try to write functions that can be replaced by their result without disturbing the application, use the type system to your advantage so that the compiler can become your ally and catch bugs at compile time, move outside world connectors behind wrappers, and leverage all of this to write powerful tests. If you would like me to write more in-depth articles for each of those elements, let me know and I would be happy to. I haven’t even talked about Category Theory, Monad, Monoid, Functors, and flatMap yet!