What are the benefits of functional programming?

What are the benefits of functional programming?

Jan 27, 2022 3:59 AM

I always wondered what the utility of purely functional programming was, particularly why someone would use it over object-oriented programming. After some reading, here are some of the benefits I learned:

  1. Because functional programming has no state, it is very easy to unit test. All unit tests are independent of each other from the start. You don’t need to check external state to verify that the function/code ran as expected, all you need to look at is the return value.
  2. Similarly, because functional programming doesn’t have state, highly concurrent applications can run on top of it without worrying about race conditions or dead lock. Parallelism is also easier because functions that don’t depend on each other can run independently.
  3. Lazy evaluation is possible with functional programming. Not everything needs to be executed in the order it is written in. Instead, it can be executed only when necessary. This is only possible in functional programming languages because they don’t have any external state that is modified.
  4. Something was mentioned about being able to hot swap code being possible with functional languages but not with typical object oriented languages. I didn’t really understand why but I’d appreciate if someone could shed some light on it.