Pure functions are:
- Easier to debug
- A good way to avoid naming collisions
- More testable
- Array methods like map and filter return a new array, which helps prevent object mutation
- Helps take the work out setting up the values in a for loop
- 'For in' has a cleaner syntax, and will only iterate over the amount of keys in the object
- Whereas a for loop can loop infinitely
While the above example is more compact and declarative, it's pretty tough to understand.
It's much better to call a separate function and use if/else blocks.
Use this instead:
For many, the above example is easier to disect. Moving to an if/else code block allows us to flatten the logic and make it more readable.
Instead, use this:
Classes are great for organizing state with its corresponding logic. Unfortunately, we're also holding state in React Components. This can create a lot of bugs that are difficult to track later on.
Functional programming practices help us eliminate state wherever possible. While cumbersome initially, this approach saves time in the long run.
Undefined is often set by the JS engine to describe variables that are undeclared or functions that haven't returned anything.
Using null lets devs know that this value was set deliberately, and sets them up for faster debugging.