Functional Programming Paradigm

Introduction to Functional Programming

Functional Programming

Senthil Nayagan
Senthil Nayagan Publication

--

Photo by Nothing Ahead from Pexels.

In computer science, functional programming is one of the programming paradigms. Here, the term programming paradigm refers to a style of programming. Common programming paradigms include:

  • Imperative — It’s the oldest programming paradigm where the program explicitly tells the compiler not only “what” a program should accomplish but also “how” to accomplish it.
  • Declarative —Only describes “what” a program should accomplish and NOT “how” to accomplish it.

Functional programming is a form of declarative programming. In contrast, most mainstream languages, including object-oriented programming (OOP) languages such as C#, Java, etc., were designed to primarily support imperative programming.

A functional approach involves composing the problem as a set of functions to be executed. Note the fundamental difference to object-oriented programming, where programs are sequences of statements, usually operating in a mutable state. Even though the sequence of statements are organized into so-called functions, they are not functions in the mathematical sense, because they do not meet some fundamental characteristics.

One of the most characteristic features of functional programming is that it treats computation in a “pure” mathematical function style, having no “side effects” and avoiding changing state, i.e., using val in place of var depending on what programming language we use.

“Object-oriented programming makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts.”Michael Feathers, author of Working Effectively with Legacy Code (via Twitter).

Developers avoid functional programming!

It’s unfortunate that the functional programming paradigm is often avoided by developers around the world, who prefer to continue developing applications using an object-oriented programming paradigm or simply using a series of statements, which is better known and more widely used. This rejection of functional programming happens due to a simple lack of knowledge. This is probably because there are a lot of new concepts that are usually explained in a principled way but full of mathematical jargon!

But the truth is that functional programming helps us solve many of the problems that arise when developing software, especially programs that require high levels of asynchronicity (the state of being asynchronous) and concurrency.

As stated earlier, functional programming falls under the declarative style of programming where it focuses on “What to solve?” in contrast to an imperative style where the main focus is “How to solve?.”

Expression vs. statement

Note that functional programming uses expressions instead of statements. Expression and statement are two different terms that are commonly misunderstood. Let’s see how one is different from the other.

In programming, an expression is a combination of values, variables, constants, operators, and functions that are combined and interpreted by the compiler to create a new value, as opposed to a statement, which is just a standalone unit of execution and doesn’t return anything. This is a common misunderstanding.

Functional programming’s selling point

A selling point of functional programming is that it enables us to write safe functionspure functions that rely only on their inputs to produce their outputs, which we can then combine together to create applications.

Concurrent programming

As we know, CPUs aren’t constantly doubling in speed any more. Instead, the CPU designers are adding more cores to CPUs to get more overall CPU cycles per second. Therefore, if we want our applications to run as fast as possible, we need to use concurrent programming techniques to put all of those cores to use, and the best way we know how to do that today is to use functional programming.

Disadvantages of functional programming

  • Sometimes, writing pure functions can reduce the readability of code.
  • Writing programs in a recursive style instead of using loops can be a bit intimidating.
  • Writing pure functions is easy, but combining them with the rest of the application and I/O operations is a difficult task.

--

--

Senthil Nayagan
Senthil Nayagan Publication

I am a Data Engineer by profession, a Rustacean by interest, and an avid Content Creator.