Why you should learn functional programming

Why you should learn functional programming

Many of the widely used languages (including C++, Java, and Javascript) are imperative.  In imperative programming, computations are structured as sequences of instructions that operate by making modifications to the state of the program.  Functional languages operate by declaring functions. The output value of a function depends only on the arguments that are passed to the function.  Because of this, functional languages have a few advantages that minimize mistakes.

“Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function.” –John Carmack

Until recently, functional languages were considered “academic” and for research only. But the industry has since discovered their advantages and many leaders are adopting functional programming to gain competitiveness.

In this post, I’ll list a few reasons why you should learn functional programming.

It makes you a better programmer

Functional programming, especially typed functional programming, offers a very different mental toolbox than the traditional programmer usually uses. For example:

Immutability

Unlike in imperative languages, in functional languages, variables are immutable by default, and do not depend on the state of the program.  Along with referencial transparency (given the same inputs, a function always return the same results), this increases consistency. As a result, we have reduced errors, increased stability, and increased effectiveness of tests. For example, tools like QuickCheck is very effective.

Type safety

Modern functional languages are usually statically typed, which means that they type check (verify all functions have the correct input types passed to them and return the correct type) at compile-time as opposed to run-time.  This ensures that all program in production are type safe.  Even though some imperative languages are also statically typed, they don’t offer as many safety guarantees as modern functional languages.

Memory safety

Functional programming languages handle allocation and de-allocation for you. The compiler avoids many common memory leaks that imperative programmers see and completely removes the risk of null dereferences.

Purity

In pure functional languages like Haskell, you can prove the absence of unwanted side-effects.  Because pure functional languages operates with functions that are type safe, you can control whether a function in the code has any interaction with the outside world more easily.

And many more! See here and here for more details.

These new tools and perspectives empower you to write better programs even when you write in traditional languages. In fact, many modern languages/extensions/frameworks have functional flavours added. See for example Rust, ReasonML and typescript. Learning functional programming will give you the necessary building blocks to pick up these framework quickly and correctly.

It’s enjoyable

A far smaller cognitive load is required when writing functional rather than imperative code. As mentioned above, in functional programming the compiler type checks and ensures memory safety. In additional, functions have no side effects. Therefore, many concerns present in imperative languages can be completely offloaded to the compiler. With this tedious cognitive load freed up, functional programmer can focus on the more fun part of programming: designing the implementation! When we develop in functional languages we often write much less code, in substantially less time, and with fewer bugs.

It gives you great career prospects

Many companies have adopted functional programming and there are many opportunities for functional programmers. Companies using Haskell include Facebook, Hasura, etc. Companies using OCaml include Ahrefs, Jane Street, etc. Check out Functional Works for many more companies that are using functional languages!

Functional languages are prominent in blockchain especially. Blockchain is an increasing popular technology with applications in many areas.  The most common uses of the technology include cryptocurrencies, banking/FinTech, and smart contracts. They all involve financial transactions that are time sensitive and mistakes can be very costly. Functional languages can minimize these mistakes and therefore many blockchain and related applications are written in functional languages! For example, Mina and Tezos are written in OCaml. Cardano, Kadena and BlockApps are written in Haskell.

Check out Blockchain Works for many more blockchain related companies that are using functional languages!

In conclusion, functional languages have advantages over imperative languages. They are already well-used in the blockchain world, and are really catching on in other areas as well. Whether you want to stay up-to-date, expand your career choices, or broaden your programming knowledge, learning functional programming is the ideal choice!

If I’ve got you convinced, I’ve got more good news for you! I’m going to publish a series of posts introducing functional programming concepts. You don’t need to have background in functional programming or even programming. I’ll go through practical concepts from beginner to advanced levels with many code examples. Stay tuned!


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.