Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 13, 2008, at 12:12 PM, Robert Cummings wrote: On Sat, 2008-09-13 at 10:09 -0400, Eric Gorr wrote: On Sep 12, 2008, at 5:13 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:51 -0400, Eric Gorr wrote: On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. As far as I know, no one has yet come up with a proof showing when debugging code is and/or is not necessary. The simple fact is that bugs can popup anywhere and spending a client's time and money by spending a few minutes writing all of the simple test cases throughout an application can be well worth it as it can save far more of the client's time and money by not wasting it on tracking down bugs that could have been easily caught. It is impractical to include debugging code for every conditional in a program. I have yet to see any evidence that it is impractical, especially after one has gotten into the habit. After all, for switch statements, adding in a default case takes mere seconds. Yes but if you do for case, you SHOULD do for if/else if/else which is an analagous approach. Doubly impractical to do so in PHP unless you have some way to prevent said debugging code from running in production. It isn't hard to prevent a code path from running in a production environment and allowing it to run in a development environment. Just one example, in PHP, would be globally defining something like PRODUCTION and then testing to see if it has a value of 1 or 0 and then writing an if statement to test the value before executing some code. There you go... you just ran a useless branch. Replacing one code path with another is hardly an optimial solution. What if your case statement is in a tight loop that runs a million times? How could that possibly matter since the code is never supposed to be executed to begin with and if it is executed it would immediately imply there is a bug? This discussion started because you said put a default statement in for debugging purposes rather than leave it empty. This suggests that you have a finite number of case statements that handle a specific set of values and that there may be values that don't need handling. Since they don't need handling the optimal path is not to have a default statement, but you suggest adding one with debugging information even though no processing need occur for some values. Now do you understand? Just because you have a switch doesn't mean all values need handling. Ah, while I had expected that my initial comments had been misinterpreted, I can see clearly now that they have. Hopefully, the messages the past couple of days have cleared things up. Furthermore, the whole point of these test cases is for those parts of the code which are never supposed to be executed to begin with, so that alone will aid in preventing said debugging code from executing in production...and if said debugging code does run in production, would that be such a bad thing (assuming it doesn't interfere with the user)? After all, because it (like the default switch case) was executed, it immediately implies there was a problem... If they're never supposed ot be executed then why are you adding extra code? That sounds like a need for better logic skills, not a need for debugging code. Because, it is never supposed to be ... not never will be. Bug's cause all kind of things to happen...including code paths that aren't supposed to happen. I doubt any client would believe it a good thing that a bug that should have been caught in development wasn't caught until production because mere minutes weren't spent putting in debug code that would have caught these bugs. You're probably one of those people that comments incrementing an index *shrug*. I mean it's mere seconds to add a useless chunk of comments that may someday help someone understand you're incrementing $i. While there is certainly a time and a place for comments which answer the questions of 'How?' or 'What?', the most useful comments nearly always answer the question of 'why?'. Why? The questions of 'How?' or 'What?' can almost always be easily determined with an analysis of the code. However, such an analysis does not always lead to understanding why code was written in the way it was written and I would strongly recommend people adopt such a comment style. Oh, for those using SCM (Source Code Management - like subversion), this policy applies to the checkin comments as well. Answering the question of w
Re: [PHP] switch case - to require the break statements seems strange to me
On Sat, 2008-09-13 at 10:09 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 5:13 PM, Robert Cummings wrote: > > > On Fri, 2008-09-12 at 16:51 -0400, Eric Gorr wrote: > >> On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: > >> > >>> On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: > > > > I don't see how that in any way makes an argument for or against. > > Once > > still must spend client's money wasting time on code that has > > questionable merit. Yes, some debugging code is a great boon in > > any > > application, but littered everywhere to fulfill someone's > > subjective > > philosophical ideal when sometimes it's just plain unnecessary... > > wasteful IMHO. > > As far as I know, no one has yet come up with a proof showing when > debugging code is and/or is not necessary. > > The simple fact is that bugs can popup anywhere and spending a > client's time and money by spending a few minutes writing all of > the > simple test cases throughout an application can be well worth it as > it > can save far more of the client's time and money by not wasting > it on > tracking down bugs that could have been easily caught. > >>> > >>> It is impractical to include debugging code for every conditional > >>> in a > >>> program. > >> > >> I have yet to see any evidence that it is impractical, especially > >> after one has gotten into the habit. After all, for switch > >> statements, > >> adding in a default case takes mere seconds. > > > > Yes but if you do for case, you SHOULD do for if/else if/else which is > > an analagous approach. > > > >>> Doubly impractical to do so in PHP unless you have some way to > >>> prevent said debugging code from running in production. > >> > >> It isn't hard to prevent a code path from running in a production > >> environment and allowing it to run in a development environment. Just > >> one example, in PHP, would be globally defining something like > >> PRODUCTION and then testing to see if it has a value of 1 or 0 and > >> then writing an if statement to test the value before executing some > >> code. > > > > There you go... you just ran a useless branch. Replacing one code path > > with another is hardly an optimial solution. What if your case > > statement > > is in a tight loop that runs a million times? > > How could that possibly matter since the code is never supposed to be > executed to begin with and if it is executed it would immediately > imply there is a bug? This discussion started because you said put a default statement in for debugging purposes rather than leave it empty. This suggests that you have a finite number of case statements that handle a specific set of values and that there may be values that don't need handling. Since they don't need handling the optimal path is not to have a default statement, but you suggest adding one with debugging information even though no processing need occur for some values. Now do you understand? Just because you have a switch doesn't mean all values need handling. > >> Furthermore, the whole point of these test cases is for those parts > >> of > >> the code which are never supposed to be executed to begin with, so > >> that alone will aid in preventing said debugging code from executing > >> in production...and if said debugging code does run in production, > >> would that be such a bad thing (assuming it doesn't interfere with > >> the > >> user)? After all, because it (like the default switch case) was > >> executed, it immediately implies there was a problem... > > > > If they're never supposed ot be executed then why are you adding extra > > code? That sounds like a need for better logic skills, not a need for > > debugging code. > > Because, it is never supposed to be ... not never will be. Bug's cause > all kind of things to happen...including code paths that aren't > supposed to happen. > > I doubt any client would believe it a good thing that a bug that > should have been caught in development wasn't caught until production > because mere minutes weren't spent putting in debug code that would > have caught these bugs. You're probably one of those people that comments incrementing an index *shrug*. I mean it's mere seconds to add a useless chunk of comments that may someday help someone understand you're incrementing $i. Similarly, for simplistic case statements, there's virtually no need for a block of debug code. > >>> Maybe you're > >>> confusing debugging code with unit tests. As I said earlier, it is > >>> far > >>> more practical to do so for complex conditions where a reader might > >>> easily get lost. Rather useless for simplistic cases. > >> > >> Until one finds it has saved hours because a problem was caught, I > >> can > >> understand why some would think that it is rather useless. > > > > I've spent hours
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 5:13 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:51 -0400, Eric Gorr wrote: On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. As far as I know, no one has yet come up with a proof showing when debugging code is and/or is not necessary. The simple fact is that bugs can popup anywhere and spending a client's time and money by spending a few minutes writing all of the simple test cases throughout an application can be well worth it as it can save far more of the client's time and money by not wasting it on tracking down bugs that could have been easily caught. It is impractical to include debugging code for every conditional in a program. I have yet to see any evidence that it is impractical, especially after one has gotten into the habit. After all, for switch statements, adding in a default case takes mere seconds. Yes but if you do for case, you SHOULD do for if/else if/else which is an analagous approach. Doubly impractical to do so in PHP unless you have some way to prevent said debugging code from running in production. It isn't hard to prevent a code path from running in a production environment and allowing it to run in a development environment. Just one example, in PHP, would be globally defining something like PRODUCTION and then testing to see if it has a value of 1 or 0 and then writing an if statement to test the value before executing some code. There you go... you just ran a useless branch. Replacing one code path with another is hardly an optimial solution. What if your case statement is in a tight loop that runs a million times? How could that possibly matter since the code is never supposed to be executed to begin with and if it is executed it would immediately imply there is a bug? Furthermore, the whole point of these test cases is for those parts of the code which are never supposed to be executed to begin with, so that alone will aid in preventing said debugging code from executing in production...and if said debugging code does run in production, would that be such a bad thing (assuming it doesn't interfere with the user)? After all, because it (like the default switch case) was executed, it immediately implies there was a problem... If they're never supposed ot be executed then why are you adding extra code? That sounds like a need for better logic skills, not a need for debugging code. Because, it is never supposed to be ... not never will be. Bug's cause all kind of things to happen...including code paths that aren't supposed to happen. I doubt any client would believe it a good thing that a bug that should have been caught in development wasn't caught until production because mere minutes weren't spent putting in debug code that would have caught these bugs. Maybe you're confusing debugging code with unit tests. As I said earlier, it is far more practical to do so for complex conditions where a reader might easily get lost. Rather useless for simplistic cases. Until one finds it has saved hours because a problem was caught, I can understand why some would think that it is rather useless. I've spent hours on bugs before, they were never once related to not having put debugging fluff into a simple set of case statements. They were almost always related to lack of comments in a complex or hackish chunk of code. Great. I hope that continues. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
Well, I've often found the need to treat several conditions with the same set of statements within a switch: switch($some_number) { case 1: case 2: { // do some shizzle break; } case 3: { // foshizzle that nizzle break; } default: { // dizzle everything else here } } You never need a break in the last case, and you don't need a default case if you know all the values you expect, although if you do have one, I believe it does have to be the last statement. Ash www.ashleysheridan.co.uk --- Begin Message --- At 10:58 AM +0100 9/12/08, Luke wrote: 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? First, the evolution of computer languages from rocks to what we have now has produced numerous logic constructs. Those that are successful, remain and appear again and again in subsequent languages. Those that are not successful fade way. As a result, all languages are converging on a successful set logic constructs. The CASE statement has been one of those concepts that has been successful and I expect it to remain -- whereas, others like DO/WHILE may fade away -- I never found reason to use it. I may be wrong, I seem to remember that the CASE statement preceded the ELSE IF statement -- and is one of the reasons why I never use it (being dyslexic is another). If I was THE PHP czar, I would drop-kick ELSE IF -- for me it's perfectly useless and confusing. YMMV. Second, as for memory leaks??? The CASE statement, nor any other logic configuration, has anything to do with memory leaks -- that's different. I am sure there are people who will disagree, but a memory leak is a condition that comes about from declaring a memory allocation (a specific size) for a variable and then later upon discarding that variable and releasing its memory back into the free memory an error is made in its size. In other words, memory leaks are accounting errors in memory allocations that reduces the amount of available free memory OR worse yet, mistakenly assigns memory to new variables that is still in use and has not been released -- all of which can crash a program. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- End Message --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 16:51 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: > > > On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: > >> On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: > >>> > >>> I don't see how that in any way makes an argument for or against. > >>> Once > >>> still must spend client's money wasting time on code that has > >>> questionable merit. Yes, some debugging code is a great boon in any > >>> application, but littered everywhere to fulfill someone's subjective > >>> philosophical ideal when sometimes it's just plain unnecessary... > >>> wasteful IMHO. > >> > >> As far as I know, no one has yet come up with a proof showing when > >> debugging code is and/or is not necessary. > >> > >> The simple fact is that bugs can popup anywhere and spending a > >> client's time and money by spending a few minutes writing all of the > >> simple test cases throughout an application can be well worth it as > >> it > >> can save far more of the client's time and money by not wasting it on > >> tracking down bugs that could have been easily caught. > > > > It is impractical to include debugging code for every conditional in a > > program. > > I have yet to see any evidence that it is impractical, especially > after one has gotten into the habit. After all, for switch statements, > adding in a default case takes mere seconds. Yes but if you do for case, you SHOULD do for if/else if/else which is an analagous approach. > Now, for a large project that has already been written, it may be > impractical but only because it is unlikely anyone will be willing to > spend the time or money to go back and put the stuff back in... If they don't want to spend the money after the fact, they probably wouldn't want to spend the money before the fact. > > Doubly impractical to do so in PHP unless you have some way to > > prevent said debugging code from running in production. > > It isn't hard to prevent a code path from running in a production > environment and allowing it to run in a development environment. Just > one example, in PHP, would be globally defining something like > PRODUCTION and then testing to see if it has a value of 1 or 0 and > then writing an if statement to test the value before executing some > code. There you go... you just ran a useless branch. Replacing one code path with another is hardly an optimial solution. What if your case statement is in a tight loop that runs a million times? > Of course, there may be other clever solutions that aren't > popping into my head at the moment. I'm sure you could come up with > something better. I already did... don't add useless debugging code when the code is simple, easily understand, and highly unlikely to contain a bug. > Furthermore, the whole point of these test cases is for those parts of > the code which are never supposed to be executed to begin with, so > that alone will aid in preventing said debugging code from executing > in production...and if said debugging code does run in production, > would that be such a bad thing (assuming it doesn't interfere with the > user)? After all, because it (like the default switch case) was > executed, it immediately implies there was a problem... If they're never supposed ot be executed then why are you adding extra code? That sounds like a need for better logic skills, not a need for debugging code. > > Maybe you're > > confusing debugging code with unit tests. As I said earlier, it is far > > more practical to do so for complex conditions where a reader might > > easily get lost. Rather useless for simplistic cases. > > Until one finds it has saved hours because a problem was caught, I can > understand why some would think that it is rather useless. I've spent hours on bugs before, they were never once related to not having put debugging fluff into a simple set of case statements. They were almost always related to lack of comments in a complex or hackish chunk of code. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 4:27 PM, Robert Cummings wrote: On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. As far as I know, no one has yet come up with a proof showing when debugging code is and/or is not necessary. The simple fact is that bugs can popup anywhere and spending a client's time and money by spending a few minutes writing all of the simple test cases throughout an application can be well worth it as it can save far more of the client's time and money by not wasting it on tracking down bugs that could have been easily caught. It is impractical to include debugging code for every conditional in a program. I have yet to see any evidence that it is impractical, especially after one has gotten into the habit. After all, for switch statements, adding in a default case takes mere seconds. Now, for a large project that has already been written, it may be impractical but only because it is unlikely anyone will be willing to spend the time or money to go back and put the stuff back in... Doubly impractical to do so in PHP unless you have some way to prevent said debugging code from running in production. It isn't hard to prevent a code path from running in a production environment and allowing it to run in a development environment. Just one example, in PHP, would be globally defining something like PRODUCTION and then testing to see if it has a value of 1 or 0 and then writing an if statement to test the value before executing some code. Of course, there may be other clever solutions that aren't popping into my head at the moment. I'm sure you could come up with something better. Furthermore, the whole point of these test cases is for those parts of the code which are never supposed to be executed to begin with, so that alone will aid in preventing said debugging code from executing in production...and if said debugging code does run in production, would that be such a bad thing (assuming it doesn't interfere with the user)? After all, because it (like the default switch case) was executed, it immediately implies there was a problem... Maybe you're confusing debugging code with unit tests. As I said earlier, it is far more practical to do so for complex conditions where a reader might easily get lost. Rather useless for simplistic cases. Until one finds it has saved hours because a problem was caught, I can understand why some would think that it is rather useless. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 16:11 -0400, Eric Gorr wrote: > On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: > > > > I don't see how that in any way makes an argument for or against. Once > > still must spend client's money wasting time on code that has > > questionable merit. Yes, some debugging code is a great boon in any > > application, but littered everywhere to fulfill someone's subjective > > philosophical ideal when sometimes it's just plain unnecessary... > > wasteful IMHO. > > As far as I know, no one has yet come up with a proof showing when > debugging code is and/or is not necessary. > > The simple fact is that bugs can popup anywhere and spending a > client's time and money by spending a few minutes writing all of the > simple test cases throughout an application can be well worth it as it > can save far more of the client's time and money by not wasting it on > tracking down bugs that could have been easily caught. It is impractical to include debugging code for every conditional in a program. Doubly impractical to do so in PHP unless you have some way to prevent said debugging code from running in production. Maybe you're confusing debugging code with unit tests. As I said earlier, it is far more practical to do so for complex conditions where a reader might easily get lost. Rather useless for simplistic cases. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 3:44 PM, Robert Cummings wrote: I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. As far as I know, no one has yet come up with a proof showing when debugging code is and/or is not necessary. The simple fact is that bugs can popup anywhere and spending a client's time and money by spending a few minutes writing all of the simple test cases throughout an application can be well worth it as it can save far more of the client's time and money by not wasting it on tracking down bugs that could have been easily caught. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 15:15 -0400, Eric Gorr wrote: > 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 > > <[EMAIL PROTECTED]> > > 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. I presumed that you wold attempt to prevent yourself from cringing and would thusly enforce such methodology upon yourself. I'm all for defensive programming where appropriate. I guess I just never realized some people need to defend against themselves at all times. > > 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. Quite true. > 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. I don't see how that in any way makes an argument for or against. Once still must spend client's money wasting time on code that has questionable merit. Yes, some debugging code is a great boon in any application, but littered everywhere to fulfill someone's subjective philosophical ideal when sometimes it's just plain unnecessary... wasteful IMHO. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
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 <[EMAIL PROTECTED]> 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
Re: [PHP] switch case - to require the break statements seems strange to me
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 <[EMAIL PROTECTED]> > >>> 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
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 <[EMAIL PROTECTED]> 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
At 10:58 AM +0100 9/12/08, Luke wrote: 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? First, the evolution of computer languages from rocks to what we have now has produced numerous logic constructs. Those that are successful, remain and appear again and again in subsequent languages. Those that are not successful fade way. As a result, all languages are converging on a successful set logic constructs. The CASE statement has been one of those concepts that has been successful and I expect it to remain -- whereas, others like DO/WHILE may fade away -- I never found reason to use it. I may be wrong, I seem to remember that the CASE statement preceded the ELSE IF statement -- and is one of the reasons why I never use it (being dyslexic is another). If I was THE PHP czar, I would drop-kick ELSE IF -- for me it's perfectly useless and confusing. YMMV. Second, as for memory leaks??? The CASE statement, nor any other logic configuration, has anything to do with memory leaks -- that's different. I am sure there are people who will disagree, but a memory leak is a condition that comes about from declaring a memory allocation (a specific size) for a variable and then later upon discarding that variable and releasing its memory back into the free memory an error is made in its size. In other words, memory leaks are accounting errors in memory allocations that reduces the amount of available free memory OR worse yet, mistakenly assigns memory to new variables that is still in use and has not been released -- all of which can crash a program. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
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 <[EMAIL PROTECTED]> > > 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. *begins weeping* Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, 2008-09-12 at 10:58 +0100, Luke wrote: > 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? Prevent memory leaks? WHAT? Even if that were an issue, implementation of switch in PHP does not look like a switch. Breaks are used to prevent fall through to the next case condition. Default is used as a catch-all... if you need a catch-all. It is perfectly valid for a case statement to fall through to the next case statement if the code can be shared. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Sep 12, 2008, at 11:39 AM, Andrew Ballard wrote: On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas <[EMAIL PROTECTED]> 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. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
On Fri, Sep 12, 2008 at 9:52 AM, Jochem Maas <[EMAIL PROTECTED]> 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. Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
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 assume that the fact that php is written in C has no baring on the functionality of php's switch implementation. *in theory* one should not have to be concerned with mem leaks when using php, that kind of low level stuff is the exact reason C isn't generally used to build web apps. in practice mem leaks can be an issue, but this is generally in long running cli scripts ... and I don't think breaks left out of switch statements have ever been shown to be the cause ... that said leaving out a break from a switch case may completely change the logic of the code, but that's a seperate issue (see tedd's explaination) 2008/9/10 Jochem Maas <[EMAIL PROTECTED]> tedd schreef: At 6:46 PM -0600 8/31/08, Govinda wrote: Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? Or is there a better reason? -G The "break" is to separate each case (i.e., condition) The switch ($i) isn't even needed if you do it like this: switch (true) { case $i==0: echo "i equals 0"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } this is 'true' ;-) and works very well when you want to check disparate truths but there are caveats: 1. it's less performant IIRC 2. there is no type checking, so auto-casting occurs during the test of each case's expression 3. it will become even less performant ... someone clever sod has a patch that heavily optimizes 'simple' switch statements ... see the internal mailing list archives for details (I can't remember the details) ... I gather this patch will eventually make it into the core, if it hasn't already. If you wanted to combine conditions, you could do this: switch (1) { case $i==-2: case $i==-1: case $i==0: echo "i is less than 0 but greater than -3 and is a counting number (i.e., no fraction)"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } Typed without checking and after my vacation. Cheers, tedd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
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? 2008/9/10 Jochem Maas <[EMAIL PROTECTED]> > tedd schreef: > > At 6:46 PM -0600 8/31/08, Govinda wrote: >> >>> Not that it is an issue, but just to understand the logic- >>> Why do we have to use 'break' statements in each case? >>> >>> switch ($i) { >>> case 0: >>>echo "i equals 0"; >>>break; >>> case 1: >>>echo "i equals 1"; >>>break; >>> case 2: >>>echo "i equals 2"; >>>break; >>> } >>> >>> all 3 cases fire, even though $i only equals ONE of those case values (if >>> I said that right). >>> I mean if $i==1, then in other languages I don't expect the first or last >>> case to fire! (?) >>> Is the purpose just so one has the OPTION of letting them all fire, and >>> turning that off with 'break'? >>> Or is there a better reason? >>> >>> -G >>> >> >> >> The "break" is to separate each case (i.e., condition) >> >> The switch ($i) isn't even needed if you do it like this: >> >> switch (true) >> { >> case $i==0: >>echo "i equals 0"; >>break; >> >> case $i==1: >>echo "i equals 1"; >>break; >> >> case $i==2: >>echo "i equals 2"; >>break; >> } >> > > this is 'true' ;-) and works very well when you want to > check disparate truths but there are caveats: > > 1. it's less performant IIRC > 2. there is no type checking, so auto-casting occurs during the > test of each case's expression > 3. it will become even less performant ... someone clever sod has > a patch that heavily optimizes 'simple' switch statements ... see > the internal mailing list archives for details (I can't remember the > details) ... I gather this patch will eventually make it into the core, > if it hasn't already. > > >> If you wanted to combine conditions, you could do this: >> >> switch (1) >> { >> case $i==-2: >> case $i==-1: >> case $i==0: >> >>echo "i is less than 0 but greater than -3 and is a counting number >> (i.e., no fraction)"; >>break; >> >> case $i==1: >>echo "i equals 1"; >>break; >> >> case $i==2: >>echo "i equals 2"; >>break; >> } >> >> >> Typed without checking and after my vacation. >> >> Cheers, >> >> tedd >> >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Luke Slater
Re: [PHP] switch case - to require the break statements seems strange to me
tedd schreef: At 6:46 PM -0600 8/31/08, Govinda wrote: Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? Or is there a better reason? -G The "break" is to separate each case (i.e., condition) The switch ($i) isn't even needed if you do it like this: switch (true) { case $i==0: echo "i equals 0"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } this is 'true' ;-) and works very well when you want to check disparate truths but there are caveats: 1. it's less performant IIRC 2. there is no type checking, so auto-casting occurs during the test of each case's expression 3. it will become even less performant ... someone clever sod has a patch that heavily optimizes 'simple' switch statements ... see the internal mailing list archives for details (I can't remember the details) ... I gather this patch will eventually make it into the core, if it hasn't already. If you wanted to combine conditions, you could do this: switch (1) { case $i==-2: case $i==-1: case $i==0: echo "i is less than 0 but greater than -3 and is a counting number (i.e., no fraction)"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } Typed without checking and after my vacation. Cheers, tedd -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
At 6:46 PM -0600 8/31/08, Govinda wrote: Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? Or is there a better reason? -G The "break" is to separate each case (i.e., condition) The switch ($i) isn't even needed if you do it like this: switch (true) { case $i==0: echo "i equals 0"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } If you wanted to combine conditions, you could do this: switch (1) { case $i==-2: case $i==-1: case $i==0: echo "i is less than 0 but greater than -3 and is a counting number (i.e., no fraction)"; break; case $i==1: echo "i equals 1"; break; case $i==2: echo "i equals 2"; break; } Typed without checking and after my vacation. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] switch case - to require the break statements seems strange to me
Govinda schreef: Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? pretty much, all code will be run inside the switch after the first case found to equate (match) until a break is hit. try it: foreach (range(0,2) as $i) { echo "\$i = $i -- running switch ... \n"; switch ($i) { case 0: echo "first case\n"; case 1: echo "second case\n"; case 2: echo "third case\n"; } } Or is there a better reason? -G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] switch case - to require the break statements seems strange to me
Not that it is an issue, but just to understand the logic- Why do we have to use 'break' statements in each case? switch ($i) { case 0: echo "i equals 0"; break; case 1: echo "i equals 1"; break; case 2: echo "i equals 2"; break; } all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'? Or is there a better reason? -G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php