I've been hearing a lot about the importance of keeping functions pure, especially when it comes to React. Some say that it helps avoid errors and makes things easier to manage. Is it really necessary to write pure functions? Or is it more of a guideline that can be bent in certain situations?
4 Answers
A general rule is that functions should ideally do one thing only. If they start doing a bunch of other stuff or calling multiple other functions, it can make tracking down bugs a nightmare. Especially if you're working with React and rendering the DOM repeatedly, those behaviors can get messy.
While you don't have to keep your functions pure, doing so usually makes them easier to manage since there aren't unpredictable interactions in play.
In React, it's generally a good idea to stick to pure functions. The documentation suggests it because pure functions allow for optimizations and make debugging much simpler. If you mark a function as pure and it's not, you could end up with some tricky bugs to track down.
Sure, you don't strictly need to write pure functions. But they definitely make your life easier in the long run. If you're in a casual setting like writing a quick script, do what works. But in larger projects, adhering to best practices pays off.
Absolutely! The complexity of a project really changes the game.

Exactly! It's like setting a trap for yourself if the function isn't actually pure.