Unit tests are a wonderful tool for programmers. They allow you to ensure an individual part of your code base performs exactly the way you intend. Unit tests are ideal for testing pure functions and therefore it is beneficial to write pure functions.
What is a Pure Function?
A pure function is a function where we can accurately predict the return value based on the arguments passed into it.*
Example of a Pure Function
The built-in PHP function strrev() reverses a string. This is a pure function. If we pass the string cat into strrev(), we can accurately predict the return value will be tac.
Every time we call strrev("cat"), we’ll get back tac.
Example of a Not Pure Function
The built-in PHP function rand( $min, $max ) returns a randomly generated number from $min to $max.
When we call rand(0, 10), we can’t accurately predict the value we’ll get back. We might get back a 5 or it might be a 7 or any other integer from 0 to 10.
Why Pure Functions are Good for Unit Testing
When unit testing we define the values we’re passing into a function and what we expect to get back.
Here is some PHP code written for PHPUnit to test a function is_palindrome().
#[DataProvider('isPalindromeProvider')]
public function testIsPalindrome( $str, $expected ) {
$this->assertEquals(
$expected,
is_palindrome( $str ),
);
}
public static function isPalindromeProvider() {
return [
[ 'racecar', true ],
[ 'salcode', false ],
[ 'Anna', true ],
[ 'Never odd or even', true ],
[ "Was it a car or a cat I saw?", true ],
];
}
Source: salcode/palindrome-notify tests/isPalindromeTest.php
Here we are asserting our expected value, e.g. true, is returned when we pass in a specific string value, e.g. racecar, to is_palindrome().
The testIsPalindrome() method receives a string ($str) and the expected value, true or false ($expected).
When testIsPalindrome() it is defined with a Data Provider Attribute setting isPalindromeProvider() as the Data Provider.
Then isPalindromeProvider() provides the array of pairs of values to test,
racecaras the string,trueas the expected valuesalcodeas the string,falseas the expected value.- etc.
More Things That Can Make Your Function Not Pure
Accessing a Value in the Database Makes a Function Not Pure
Code that does something like
function get_title() {
return $db->query("SELECT value FROM options WHERE name='site_name'");
}
would be a not pure function.
Just looking at our call to our function, we can’t predict what value will be returned. We have to go look elsewhere, in the database, to determine the return value.
Making a Remote API Call
Similar to retrieving a value from the database, when retrieving a value from a remote API we can’t accurately predict the return value.
Accessing a Global Variable
This function would not be a pure function.
function get_wp_version() {
global $wp_version;
return $wp_version;
}
Writing Code with Pure Functions
When writing code, I look for opportunities to extract functionality into pure functions, then I can write unit tests against these pure functions.
Extracting functionality into pure functions not only makes my code easier to unit test but I’ve found it improves the quality of my code as well.
The Full Definition of Pure Function*
I’ve been focusing on how a pure function is predictable and consistent in the values it returns.
There is a second part to the definition of a pure function in that it has no side effects. A pure function can not make changes outside itself. For example a pure function can not write a value to the database, or set a global value, or modify any variable outside of itself.
Leave a Reply