Re: switch case - to require the break statements seems strange to me
| From: | Robert Cummings | Date: | Fri, 12 Sep 2008 18:51:05 +0000 |
| Subject: | Re: switch case - to require the break statements seems strange to me | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-280039@lists.php.net to get a copy of this message | ||
On Fri, 2008-09-12 at 14:33 -0400, Eric Gorr wrote:
> On Sep 12, 2008, at 2:14 PM, Robert Cummings wrote:
>
> > On Fri, 2008-09-12 at 11:47 -0400, Eric Gorr wrote:
> >> On Sep 12, 2008, at 11:39 AM, Andrew Ballard wrote:
> >>
> >>> On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas <jochem@iamjochem.com>
> >>> wrote:
> >>>> Luke schreef:
> >>>>>
> >>>>> I wonder if this is a shared trait between C and PHP (since I
> >>>>> understand
> >>>>> PHP
> >>>>> is written in C) that the break; and the default: are placed for
> >>>>> good
> >>>>> practice in all switch statements since they prevent memory leaks?
> >>>>
> >>>> default is not required, never heard it was good practice to always
> >>>> put it
> >>>> in.
> >>>
> >>> I can't say I've ever heard it recommended as good practice from the
> >>> standpoint of performance in any specific language I've ever worked
> >>> with, but I have heard people suggest that you always include an
> >>> explicit default case in any kind of branching logic. It does seem
> >>> useless to say
> >>>
> >>> default: // do nothing
> >>> break;
> >>>
> >>> in a switch block, but I imagine the reasoning behind it is so that
> >>> anyone who reads your code can see that you actually thought about
> >>> what should/would happen if none of the other conditions were true
> >>> rather than ignoring those conditions.
> >>
> >> It is always useful for a 'default:' case, which would normally do
> >> nothing, to include some debug-only code so you can be notified if
> >> the
> >> default case is ever hit. Whenever I see an empty or just missing
> >> 'default:' case, I always cringe.
> >
> > Whenever I see pointless debug fluff just for the sake of not having
> > an
> > empty or omitted default statement I cry.
>
> It's only pointless debug fluff until it saves untold numbers of hours
> which would have been spent attempting to track down a bug that would
> have been caught by the now critical test which took only about 10
> seconds to write.
Maybe for complex cases... but you used a broad brush when you advocated
always doing this and so I most point out the pointlessness of debug
fluff for simple use cases. Since case statements are for the most part
equivalent to if/else if/else statements, I must ask if you also enforce
an else statement on all such conditionals. Such debugging should be
done on a case by case basis depending on the particular scenario at
hand. If the code is simple and obvious then debugging serves little
purpose but to convolute the code.
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP