Here’s a PHP example for shortening your conditionals and parameter checking. (There is probably a design pattern for this, but I haven’t found a name for it yet. Submit a comment if you know the name.)
This is a typical PHP example.
/* A useful function.
@param $arg1 First parameter.
@param $arg2 An optional array.
@return Returns a useful string.
*/
public function doSomethingUseful($arg1,$arg2=array())
{
if (isset($arg2))
{
foreach ($arg2 as $v) { /* ... */ }
}
else
{
return '';
}
}
Here’s a way to shorten and simplify.
/* A useful function.
@param $arg1 First parameter.
@param $arg2 An optional array.
@return Returns a useful string.
*/
public function doSomethingUseful($arg1,$arg2=NULL)
{
if (is_array($arg2)) { return ''; }
foreach ($arg2 as $v)
{
/* ... */
}
}
No comments:
Post a Comment