Only the Purest intentions.
So, what is a Pure function?
Well a pure function gives a lot away in its name. A pure function is a function that produces output that’s purely based on its input. Nothing Else. You take whatever arguments or parameters you set in the function and that’s where you get your result from. Another stipulation for being considered a true pure function is that it has to be free from any side effects.
- A side effect could be something like printing an output to the console, or storing an output.
- On the topic of storing, any information saved to a database in the function is also considered a side effect.
- Having any effect on any variables defined outside of the scope of the function.
- Retrieving data from within the function that didn’t come from the arguments.
So why use these functions?
Well, that’s a nuanced answer but to put it simply: pure functions mean consistency. Unlike other functions that can have their outputs manipulated by changing data sets, you can rely on a pure function to provide the same output with the same input. It gives us leeway as programmers when we don’t have to consider a slew of outer dependencies. Pure functions are also invaluable because they leave outside data alone. One of the biggest problems I’ve run into in my time programming is having unexpected changes in variables because of a tweak I put in a function somewhere and didn’t consider. Pure functions give you the data you want without messing anything else in your code base up. They’re purely data outputs, not data manipulation so you can rest easy that they won’t interfere with other parts of your code.
Pure functions also make testing easier because since they provide consistent output, you can refactor the arguments and expect a clear output. What this means as a programmer is that the validity of the code is easier to identify. If the result we expect is not achieved then it’s usually easier to figure out the problem (usually syntax in the pure function, or a problem with functions passed in as parameters) since it’s so isolated. This is in stark contrast to how I’m sure many of us have spent numerous hours poring over pages of code for an argument or syntax error.
So if it’s so useful how come we don’t see it more often?
Well, the truth is, we do see it pretty often. We just don’t think of them as pure functions. Pure functions do have their limitations. As mentioned pure functions do provide consistent output, which has its advantages but can be problematic when trying to get a varied data output. Additionally pure functions can be difficult for testing when you specifically want to break your code to identify weaknesses. Also pure functions enjoy the benefits of being free from outside manipulation, but as programmers we sometimes want to be able to adjust variables mid method so as to whittle away at a data set, such as in a recursive function.
So what did we learn?
Well, for me it was the realization that I had been building pure functions in my code over time, just not identifying what exactly they were. Now that I understand their structure and advantages though I’ll be sure to make use of them properly to optimize my code.