What is functional programming
Functional programming is a programming paradigm where programs are constructed using few principles. Let’s see what those principles are and how it is better than the regular oop.
-
Declarative
- Focus on what rather than how, we have seen functions like
filter
,map
,forEach
. - These methods focus on what it does rather then how it does the operation.
- Focus on what rather than how, we have seen functions like
-
Pure functions
- Function which always gives the same output based on the input. Call this function a thousand times and you will get the same output every single time.
- Pure functions will be independent of the outside world.
- Pure functions will not be having any side effects, no more surprises of figuring out why the boolean was changed when you were calling particular function
- Pure functions are easier to test. Since its independent and has no side effects
-
Immutability
- Your model classes should be immutable
- Want something different?. Make a new one
-
Concurrent
- Pure functions can be executed in any order since those operate on immutable objects and will not have any side effects
- No unexpected behaviour
-
Higher order functions
- Functions are first class citizens
- Pass function to a function
- Return function as a result from another function
Until next time