I want to motivate you to start caring about functional programming! In this article series I will do that by identifying certain areas of traditional object-oriented languages where the code often does not fully satisfy us. Using functional code for the same purposes allows us to better express our intent.
I want to show you how switching to a functional paradigm – thinking in functional style – will make whole classes of problems disappear, especially when it comes to multi-threaded programming.
First off: Immutability
An immutable object is an object whose state cannot be modified after it is created. In multi-threaded applications that is a great thing. It allows for a thread to act on data represented by immutable objects without worrying what other threads are up to. In short: immutable objects are more thread-safe than mutable objects.
Unlike traditional object-oriented languages, functional programming, by its very nature, encourages us to write thread-safe code. In this article I am going to demonstrate this by using something as trivial as variable assignment.
Although I will be using F# in my examples, the point is not to address specific aspects of F# or .NET. Quite the opposite: The choice of functional language is not important here. It could just as well have been Scala, Ocaml or Haskell. The point is neither to give a comprehensive introduction to language features: There are plenty of books that do that better.