Refactoring Toward Pure Functions
Explore techniques to refactor Elixir functions calling external dependencies into pure functions. Learn how this approach simplifies testing by isolating logic, reducing complex dependency setups, and improving code organization and maintainability using ExUnit.
We'll cover the following...
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, ...