Spread the love

“Become a better developer with functional programming”. Many articles currently call for people to follow the functional order. 

What is functional programming? Is the functional programming style right for you? Which programming languages ​​use it as a paradigm? Do you have to use a highly academic language like Haskell? Our article answers these questions.

Where does the functional programming trend come from?

A few years ago there was a call for object-oriented programming. After PHP 5 came out, it was a good time to upgrade the quality standards for PHP code. Away from free programming and spaghetti code and towards better maintainability. Everything became objects.

Today it is the evolution of ECMAScript, better known under the name of (functional) JavaScript, that brings about a trend reversal. Version ES6, released in 2015, brings countless improvements. 

Angular, React and Vue are the new front-end frameworks for web projects and apps. JavaScript libraries like jQuery are scrap heap. This change brings again the call for a better code structure.

What is functional programming?

A programming paradigm that was developed in the 1930s and is based on mathematical foundations. 

Wait, don’t all programming languages ​​do this?

var x = 1;
x = x + 1;

A code example that makes math teachers desperate. Although x = x + 1 is impossible in algebra and arithmetic, this code is used in thousands of programs. 

In a functional programming language, however, this line of code is illegal.

Pure functions

Purely functional programming languages ​​require pure functions. A pure function

  • uses an input value,
  • returns the same output value,
  • has no side effects,
  • observes the immutability of variables.

Side effects

Simple side effects are a console.log or the saving of a variable in the database. Oops, some developers might think. I don’t know of any alternative. The point is not to forbid console.log, but rather to mix calculation and storage in one and the same function.

Immutability

The immutability of variables is a prohibition that is not limited to pure functions. Variables are generally not changed in functional programming languages. Instead, a copy is made for the changed value.

Recursion instead of loop

Since the immutability affects simple counters in loops, functional programming does not use for, while, do and repeat. Instead of these loop constructs, recursion is used:

function sumRange (start, end, acc) {
if (start> end)
return acc;
return sumRange (start + 1, end, acc + start)
}

Higher-order functions

Higher-order functions can use a function as a parameter. It is thus possible to use a pure function for calculation in an equally pure function for storing a data record.

Why program functionally?

What sounds like an unnecessary effort at first is not only much cleaner, but it simplifies debugging and writing unit tests. Last but not least, it increases the maintainability of the code immensely.

The multiparadigmatic programming language JavaScript is developing rapidly. The versions 2016, 2017 and 2018 clearly show the evolution of the script language. 

The often criticized fault tolerance will give way from version to version to the need for clean code structures and programming paradigms.

Areas of application for functional programming

In addition to functional JavaScript and purely functional programming languages ​​such as Haskell, C #, Python, Java, and PHP are also suitable functional programming languages

For beginners and those switching, it is worth taking a look at the young programming language Elm, which compiles to JavaScript, Haskell, OCaml, F #, or Clojure.

Learn functional programming with Elm

Rushing into every programming trend and all new languages ​​is neither possible nor useful. However, a little digression into a purely functional language like Elm will help improve his programming style. Code becomes more readable, understandable and maintainable.

It is good practice to rewrite a small program or part of a program with Elm. After refactoring, compare your compiled Elm code to your original source code. The simplification of the code is clearly visible. Many programmers have made a big leap with Elm. Some even stayed with Elm.

What is functional programming: Conclusion

Anyone who knows how to use the various programming paradigms can better decide which code structure is suitable for a project and which language is usually better. 

No approach is suitable for every task. Experienced programmers who make the right decision in the conception phase generate maintainable code and will be happy to work on the project years later.

Newsletter SubscriptionYou don't wanna miss our exclusive guides

Get Exclusive Online Business Guides and Tips That I Only Share With Email Subscribers


Spread the love

Leave a Reply

Your email address will not be published.