Functional programming has been one of those topics that is popular for developers to study for a while now. Unfortunately most developers are not using a functional language to use at work, so this reduces the possible adoption, even when team members agree that it would make things better.
The good news is that we don't have to drop Java for Haskell in one single swoop. We can adopt functional programming little by little, building functional habits. You can do this even if you work on OOP languages.
Write programs using functions
Follow the following rules Write functions that are
This is a gross simplification of functional programming. Yet if you start building up this habit, it will take you a long way towards functional programming. And it might make your OOP code easier to test as well.
A common pattern in a class is to have object variables that we modify in methods.
1 2 3 4 5 6 7 8 9 10 |
|
This is common OOP code. .damage()
doesn't return a value. The pros for this style is small, understandable code.
Now, let's make it more functional.
1 2 3 4 5 6 7 8 9 10 |
|
Now damage()
only does a calculation and returns the result. It doesn't depend on the object's current state. We can use damage()
for anything. It should be easy to test.
The dicerning reader will notice that we are still updating state. You are right! Most programs work on state, and you will always have some methods that updates state using the pure methods. These kind of methods are often called procedures.
Your code will improve if you do nothing else but adopt this habit.
Most popular languages have some kind functional features around lists or arrays. You can learn how to use them.
In Python, learn how to work with list comprehensions. Learn the Enumerable methods in Ruby. Learn LinQ in C#.
By learning these, you are getting a good handle over the basic functional methods, map
, filter
, reduce
.
Remember to express these in an idiomatic way. Python has map
and filter
, but currently many don't think it is idiomatic. C# should still look like C#.
Most of us don't have the luxury to switch languages from Java to OCaml. But all of us have to work with JavaScript.
JavaScript accepts functions as arguments. It is such a common pattern, that we don't even think about it as something special.
In JavaScript you can write modules, bypassing objects, if you wish to do that.
Here you can can learn how to curry a function. If you are adveturous, you can use currying as a way to learn function composition.
This is the immersion part. What we were doing before is like speaking in English and throwing in some Spanish as you practice. Sometimes you will need to jump into pool and swim.
I recommend learning Racket. Racket is a descendent of Scheme, a Lisp, and has excellent documentation. It has many capabilities, so you can craete games, scripts, web servers, or gui applications.
That said, learn whatever you like. Elixir and F# also have a lot of books and documentation. Ocaml and Haskell are some other options.
This one can happen at the same time as you are adopting the previous habits.
You can do a number of functional practices in any language. But if you truly like functional programming, then you should adopt a functional language.
Adopt the functional language that your team likes the most. Resist the temptation of rewriting everything. Instead, create new script and software with your functional language.
One common mistake when learning a new technique is to attempt to use it in the wrong place. Lisp is cool, but your Django code shouldn't look like it was written in Lisp. You probably can bend python enough to make it look like Lisp if you squint enough, but other people, including you in 6 months, won't understand it.