Re: switch case - to require the break statements seems strange to me

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Sep 12, 2008, at 2:51 PM, Robert Cummings wrote:

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@xxxxxxxxxxxxx>
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.

Even in the simple cases, it is always useful to employ defensive programming techniques because it may catch a bug which would have otherwise taken hours to track down.

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.

Who said anything about enforcement?

I simply said it make me cringe and stated an opinion which is derived from an accepted ideal that defensive programming is a good thing.

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.

"Convoluted" is a relative and subjective term.

Once one gets used to writing and maintaining code written this way, it is no longer convoluted. Furthermore, every decent editor these days would allow one to collapse such code for those who find it difficult to interpret...providing even a stronger reason to write it in the first place.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux