Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created August 24, 2018 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/beb35925fb35b7b19d04b7fbd293a56a to your computer and use it in GitHub Desktop.
Save kellenmace/beb35925fb35b7b19d04b7fbd293a56a to your computer and use it in GitHub Desktop.
<?php
$projects = wds_get_projects();
// ...do some work...
$active_projects = array_filter( $projects, function( $project ) {
return 'active' === $project['status'];
});
// ...do some work...
$active_microsoft_projects = array_filter( $active_projects, function( $project ) {
return 'microsoft' === $project['client'];
});
/*
Because we're setting the values of these variables once and then never
changing them (a.k.a. leaving them immutable), a developer unfamiliar
with this project could open up this file and easily determine what each
of them is equal to at any point. That would NOT be the case if the $project
variable started off storing all of the projects, then later on it was
changed (mutated) to represent just the active projects, then later on it
was changed again to represent the active Microsoft projects.
Immutability FTW!
P.S. Breaking this up into smaller, single-purpose functions would be even
more of an improvement and would rule out the need for so many variable
names in many cases.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment