Refactoring Toward Pure Functions

Learn about refactoring logic into pure functions to simplify testing.

Function calls to external dependency

When code has an external dependency, it rarely acts as a straight pass-through of the values of that dependency. Mostly, when a dependency is called, the response is manipulated before returning the result to the function call. The more manipulation our code does of that data, the better a candidate our code is for refactoring the logic into a pure function.

Let’s look at a visual representation of a function that calls out to a dependency and manipulate that data before returning its response.

The section of our drawing labeled “manipulate data” is code that changes the data without external dependencies. Testing this function can be pretty painful if the code has significantly different outcomes, ...