The easiest way to make your existing code more functional
This is an episode of Thoughts on Functional Programming, a podcast by Eric Normand.
What is the easiest way to make an existing functions more functional? It's quite a simple technique and you can apply it today.
Eric Normand: What is the easiest way to get started in functional programming and to make your existing code just a little bit more functional today?
Hi, my name is Eric Normand. These are my thoughts on functional programming. I think the easiest refactoring you can do right now is the following. Find a function that reads in global mutable variables.
You probably have a few and it's being called somewhere. Make sure that this function doesn't do any other actions. Remember, reading a global mutable variable is an action because it depends on when you run it, you're going to get a different answer depending on when you read that value because it can change over time.
What you want to do is take that function. Make sure there's no other actions in it besides reading global variables. Go through and find each time you read a global variable. Make that an argument to your function. Instead of reading from the global variable, you're just going to read from the argument. You can even use the same name as the global variable. That's fine.
You do that for all your reads of global variables, then you have to go to the functions that call that one. Instead of calling it with the old arguments, they are going to read the value from the global variables and pass those in. You've moved the responsibility of reading the global variables up one level of the calls. What you had before was an action that called an action.
You had some function that called this function that you just changed. Both of those were actions. The function that you just added all this arguments to, that one was an action because it read from global mutable state. The function that called that one was an action, because it called that one. Now, you've moved all the actions out of this function. Now, it's a calculation.
Now, it is a pure function from arguments to return value. Therefore, you've made your total code base incrementally a little bit more functional. You have more code that's in a calculation and less in actions. That means this function is now more testable because you can call it as many times as you want. You don't have to setup these global variables in their environment.
Often, one of the reasons why these kinds of functions are so hard to test is because we don't realize all the possible states that could exist in those global variables. There's always code that could write to that anytime. We just don't think of all the values. Now, you can test it a lot more thoroughly because it's all self-contained.
I hope that helps you make your code a little bit more functional. You can always get in touch with me if you have any questions or any suggestions. I'm on Twitter @ericnormand. You can also email me anytime. I love getting into discussions. My email address is firstname.lastname@example.org. Thank you very much.