[nycphp-talk] structured programming in practice
Chris Bielanski
Cbielanski at inta.org
Wed Jun 15 10:05:40 EDT 2005
I agree with Dan, and I wanted to inject a little experience here. I
spent a few years doing Delphi programming and it got me into a habit of
declaring and using $result in most of my functions. Now, whereas I use
multiple-exit logic like most folks here, a $result variable does (IMHO)
lend itself to the SP method of single-exit. Perhaps this is a suitable
"hybrid" approach, if that's the correct term?
My example modified from Dan's: (I went with my habit for case syntax
function make_beverage_cb($bev_type, &$bev) {
$result = false;
switch ($bev_type) {
case ('coffee'): {
if (add_cwater($bev) && brew_coffee($bev)) {
$result = true;
}
break;
}
case ('tea'): {
if (add_cwater($bev) && add_tea_bag($bev)) {
$result = true;
}
break;
}
default: {
$result = false;
break;
}
}
return $result;
}
Thanks,
Chris Bielanski
Web Programmer,
International Trademark Association,
655 Third Avenue, 10th Floor
New York, NY 10017-5617 USA
+1 (212) 642-1745, f: +1 (212) 768-7796
mailto:cbielanski at inta.org, www.inta.org
INTA -- 125 Years of Excellence
Dan Said:
> That aside, the notion of having one exit point per function
> doesn't carry much weight in my mind. If one reaches the end
> of execution, it seems clearest to me to actually say you've
> reached that point by leaving the function. Otherwise,
> you're left wondering what else happens down in the rest of
> the function and have to go hunting, only to find out, oh,
> it's done. What a waste.
>
> Something I noticed in your examples is you're not handling
> the situation if $bev_type isn't coffee or tea. I'd likely
> use a switch with a default case here.
>
> You're assigning the returns of add_twater() and
> add_tea_bag() to variables, but the values are only used for
> evaluation in an if statement.
> It's more efficient to just do:
>
> if (!add_tea_bag($bev)) {
> return false;
> }
>
> Now, as far as how to handle reporting of errors, that's
> really situation specific.
>
> Here's how I'd write your function:
>
> function make_beverage_dan($bev_type, &$bev) {
> switch ($bev_type) {
> case 'coffee':
> if (add_cwater($bev) && brew_coffee($bev)) {
> return true;
> }
> return false;
>
> case 'tea':
> if (add_cwater($bev) && add_tea_bag($bev)) {
> return true;
> }
> return false;
>
> default:
> return false;
> }
> }
>
> I notice you're passing in $bev by reference. If that's
> solely to get the value out of the method, I tend to prefer
> returning the values.
>
> --Dan
>
> --
> T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y
> data intensive web and database programming
> http://www.AnalysisAndSolutions.com/
> 4015 7th Ave #4, Brooklyn NY 11232 v: 718-854-0335 f:
> 718-854-0409 _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org
>
More information about the talk
mailing list