mardi 30 décembre 2014

Should we refactor our existing codebase to use functional programming, especially streams?


We have a large project written in PHP. It almost exclusively uses imperative operations, as in example 1. Should we refactor our existing code to use functional operations? Do you think that the code in example 2 is easier to maintain?


Example 1 - imperative



$cookieString = '';
foreach($cookieArray as $cookieName => $cookieValue) {
if($cookieString != '') {
$cookieString .= '; ';
}
$cookieString .= $cookieName.'='.$cookieValue;
}
return $cookieString;


Example 2 - functional



return KeyedStream::of($cookieArray)
->mapEntriesToStream(function($cookieName, $cookieValue) {
return $cookieName.'='.$cookieValue;
})
->joinToString('; ');
}




Aucun commentaire:

Enregistrer un commentaire