Why You Should Learn Functional Programming
Tags: haskell, ocaml, blockchain, typedfunctionalprogramming
Published: 2021-11-26 17:20:17

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 referential 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](https://hackage.haskell.org/package/QuickCheck) are 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 programs 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 operate 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](https://www.foxhound.systems/blog/why-haskell-for-production/)
and [here](http://book.realworldhaskell.org/read/why-functional-programming-why-haskell.html) 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 extensions/frameworks have
functional flavours added. See for example [ReasonML](https://reasonml.github.io/) and [typescript](https://www.typescriptlang.org/). Learning functional programming will give you the necessary building blocks to pick up these frameworks 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 addition, 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, the 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](https://hasura.io/), etc. Companies using OCaml include [Ahrefs](https://ahrefs.com/), [Jane Street](https://www.janestreet.com/), etc. 

**Check out [Functional Works](https://functional.works-hub.com/) for many more companies that are using functional languages!**

Functional languages are prominent in blockchain especially. Blockchain is an increasingly 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](https://minaprotocol.com/) and [Tezos](https://tezos.com/) are written in OCaml. [Cardano](https://cardano.org/), [Kadena](https://www.kadena.io/) and [BlockApps](https://blockapps.net/) are written in Haskell.

**Check out [Blockchain Works](https://blockchain.works-hub.com/) 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 a background in functional programming or even programming. I'll go through practical concepts from beginner to advanced levels with many code examples.

Stay tuned!

Originally written by [Marty Stumpf](https://www.works-hub.com/users/8a724570-86e7-4b45-bcb0-b838f2f226ea?utm_source=dev_to&utm_medium=blog_xpost) for [WorksHub](https://www.works-hub.com/learn/why-you-should-learn-functional-programming-50371?utm_source=dev_to&utm_medium=blog_xpost)