Re: [PHP] IF or SWITCH (asinine?)
On Friday 07 April 2006 16:41, tedd wrote: > Note, it's not actually required to have a variable to be evaluated > for each case statement. Each case statement stands on it's own > independent from the evaluation. > > This has always worked for me in other languages and I am assuming > that it works the same in php. > > Does anyone disagree or will correct me? > > HTH's. > > tedd You are right. It does work that way. My opinion just differs in that I think ELSEIF works better in that situation than SWITCH, and SWITCH is better for a single value comparison than ELSEIF. I think that by now people will have been able to form their own conclusions as to which style they prefer, and either is probably more readable than putting everyone all on one line :-P -- Ray Hauge Programmer/Systems Administrator American Student Loan Services www.americanstudentloan.com 1.800.575.1099 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH (asinine?)
Anyways, I think this debate is pretty much pointless now, you've clarified your point of view (sort of) and I'll just run with the final though that you limit your use of switch to case where elseif would be needed... and to clarify on my part once more, I did not call that asinine. Cheers, Rob. Rob: Okay. Please accept my public apology for our misunderstanding. tedd -- http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH (asinine?)
> Personally, as I said before, I use if/else for two conditions and switch for three or more. Your mileage may vary, but it's truly a > matter of choice and is not one of asinine methodology. > tedd I think that's a fairly good methodology to follow. I usually do that... except when I run into a huge list of multiple conditions for each possibility (TRUE && FALSE OR FALSE && TRUE) etc. To me IF/ELSEIF is easier to ready in that case, The true && false scenarios require me to work up a truth table, for I have great difficulty figuring that out in my head. and SWITCH is a LOT easier to read if you have one variable and have a CASE for each possibility of that variable. I believe (until someone informs me otherwise) that your statement above is a common misconception of the switch conditional -- you don't need a single variable to evaluate. Case in point, try this (taken from your own code). $who_cares = 1; switch ($who_cares) { case $numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal: $retVal = array(TRUE, 'A'); break; case $numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && $enumFFELP + $numFFELP == $numTotal: $retVal = array(TRUE, 'A'); break; } Note, it's not actually required to have a variable to be evaluated for each case statement. Each case statement stands on it's own independent from the evaluation. This has always worked for me in other languages and I am assuming that it works the same in php. Does anyone disagree or will correct me? HTH's. tedd -- http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH (asinine?)
On Fri, 2006-04-07 at 11:57, tedd wrote: > Ray said: > > >I'm pretty sure he's ONLY talking about IF/ELSEIF and not IF in general. > >That's what I got from the message. Correct me if I'm wrong. > > Of course, you're absolutely right -- I said specifically IF/ELSEIF. > My objection is not with the IF statement, of course I use that. In > fact, I use if/else for two choices and switch for three or more. > > I find it interesting that some programmers, who have to be exact > with their syntax, can't read a simple statement containing proper > syntax without misreading it. Everyone is fallible, don't pretend you are not. When I realized my misunderstanding I made it public and requested clarification. Either way, I wasn't the only one to make the mistake as John Nichel also assumed you included the if structure in your diatribe. Undoubtedly others perceived similar. > Rob said: > > >I think I may have read too much into your previous post. Are you saying > >specifically that you never use the elseif construct or any of the if, > >elseif, else constructs? If the latter then I think your methodology is > >somewhat asinine since I'm sure 99% of the programmers out there would > >view code consisting entirely of switches in place of ifs with a huge > >grain of WTF :) > You read too much? No, the case is that you didn't read enough -- or No I read ALL of it. See, you're being unclear again, what you mean to say above is that I didn't absorb the full meaning. > you didn't comprehend what I wrote. I said that I NEVER use the Yes there we go, I didn't FULLY comprehend what you wrote. > IF/ELSEIF construct. I did not say that I don't use IF/ELSE (forgive So does that mean you DO use IF/ELSEIF/ELSE? I mean come on, I can only assume you do since you don't attach the ELSE to your list. What? I'm supposed to read your mind? Oh sorry, I bow to your authority. > my double negative). Don't be what you called my methodology. If you understood what I wrote you would see I didn't call your methodology asinine, I called restrictive use of switch in place of if, if/elseif, if/else, if/elseif/else asinine. Oh wait, maybe I'm misunderstanding you again and you don't just restrict to elseif usage. Dammit, you've muddied the water again. > As for what the 99% of programmers out there -- Locus ab auctoritate > est infirmissimus. "The argument from authority is the weakest." *Wh* Who's the authority btw? You? I mean, you've got 30 years on me. I'm not sure why you're weakening your stance, but whatever works best I guess. > Also would you care to provide proof for your statistics? Right off the top of my head. Thank you, thank you very much. It was not meant as a definitive statistic, it was meant as a comment on the unlikelyhood of anyone naturally subscribing to replacement of the complete set of if/elseif/else permutations with switch statements. Anyways, I think this debate is pretty much pointless now, you've clarified your point of view (sort of) and I'll just run with the final though that you limit your use of switch to case where elseif would be needed... and to clarify on my part once more, I did not call that asinine. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH (asinine?)
On Friday 07 April 2006 08:57, tedd wrote: > > Personally, as I said before, I use if/else for two conditions and > switch for three or more. Your mileage may vary, but it's truly a > matter of choice and is not one of asinine methodology. > > Thanks to all who replied. > > tedd > > -- > --- >- http://sperling.com I think that's a fairly good methodology to follow. I usually do that... except when I run into a huge list of multiple conditions for each possibility (TRUE && FALSE OR FALSE && TRUE) etc. To me IF/ELSEIF is easier to ready in that case, and SWITCH is a LOT easier to read if you have one variable and have a CASE for each possibility of that variable. Just my opinion of course :) I guess when it came down to it I just had to stick with what works for me. I cleaned up a lot of the totals and counting, so it's MUCH easier to read now :) To imitate Tedd... Thanks to all who replied -- Ray Hauge Programmer/Systems Administrator American Student Loan Services www.americanstudentloan.com 1.800.575.1099 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH (asinine?)
Ray said: I'm pretty sure he's ONLY talking about IF/ELSEIF and not IF in general. That's what I got from the message. Correct me if I'm wrong. Of course, you're absolutely right -- I said specifically IF/ELSEIF. My objection is not with the IF statement, of course I use that. In fact, I use if/else for two choices and switch for three or more. I find it interesting that some programmers, who have to be exact with their syntax, can't read a simple statement containing proper syntax without misreading it. --- John C. Nichel IV said: I remember IF constructs from BASIC and PASCAL, but no switch statements (somebody correct me if I'm wrong). But what I'm wondering is how in the world did you do conditional checking if there were no switches, and you don't use IF's? Did you not code error handling, different cases based on user input, status of a data stream, etc in all the years prior to something like switch being introduced??? I'm no programming historian, but from memory, Fortran, BASIC, and PASCAL all started without a "switch" construct. However, later-on BASIC most definitely provided a SELECT/CASE and PASCAL had a case operation of some type -- as for FORTRAN, it's been a long time since I did anything in it, I don't know. As for how did I did conditional checking, I did it like every other programmer of that day -- and without ELSE/IF's. You certainly can't use the construct if it's not there -- using IF's sufficed and as I said above, I NEVER said that I don't use if/else -- what I said was I don't use if/elseif. Actually, by you misreading my post, you provided support for my claim that the use of if/elseif is confusing and thus less readable. Clearly if I had said that I don't use the switch, there would have been less confusion on your part, right? --- Rob said: I think I may have read too much into your previous post. Are you saying specifically that you never use the elseif construct or any of the if, elseif, else constructs? If the latter then I think your methodology is somewhat asinine since I'm sure 99% of the programmers out there would view code consisting entirely of switches in place of ifs with a huge grain of WTF :) You read too much? No, the case is that you didn't read enough -- or you didn't comprehend what I wrote. I said that I NEVER use the IF/ELSEIF construct. I did not say that I don't use IF/ELSE (forgive my double negative). Don't be what you called my methodology. As for what the 99% of programmers out there -- Locus ab auctoritate est infirmissimus. Also would you care to provide proof for your statistics? --- Kevin provided: benchmarks can be hazardous, but lets look at them at their most basic level. By this I mean how folks use them every day... http://www.phpro.org/benchmarks/if-switch-benchmark.html Thanks for the link. That fits with my tests -- however, the speed difference, which in not significant IMO, is not at issue with me -- it's the readability. To me, the switch wins hands down in readability over the multiple if/elseif's. However, I realize that's a personal decision and do not claim good/bad practice in using either. --- M said: If it can be used, I'd go for the clarity of switch. I have yet to find a single problem where a switch can't be used to replace multiple if/elseif's. I even posted an open query to this list in that regard, and while at least one was compelled to call my methodology asinine, no one could provide an example otherwise. So my claim stands that you simply don't need the if/elseif construct -- for it can be totally replaced with the switch. Now, someone prove me wrong -- please -- I would like to know why the construct is still around after being the evolutionary step between if/else and switch. Is it just habit, or what? Personally, as I said before, I use if/else for two conditions and switch for three or more. Your mileage may vary, but it's truly a matter of choice and is not one of asinine methodology. Thanks to all who replied. tedd -- http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
This one time, at band camp, Jad madi <[EMAIL PROTECTED]> wrote: > Kevin, > I'm just curious to know how to did you do that benchmark. Sure, use ab (Apache Benchmark) which comes with your build of apache. Simply create your file foo.php and give ab the command ab http://www.example.com/foo.php -n 1 -c 4 you may also use localhost as such ab http://localhost/foo.php -n 1 -c 4 If the concurrency is omitted a concurrency of 1 is assumed This will tell ab the url, and to request it 10,000 times with a concurrency of 4. Depending on your hardware ab will tick over and generate a report. man ab will give you more info Kind regards Kevin -- "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
Kevin, I'm just curious to know how to did you do that benchmark. On Fri, 2006-04-07 at 12:40 +1000, Kevin Waterson wrote: > This one time, at band camp, Robert Cummings <[EMAIL PROTECTED]> wrote: > > > I'm gonna go out on a limb here and say WRONG! > > > > Run yourself a benchmark. > > benchmarks can be hazardous, but lets look at them at their most basic level. > By this > I mean how folks use them every day... > > http://www.phpro.org/benchmarks/if-switch-benchmark.html > > Kind regards > Kevin > -- > "Democracy is two wolves and a lamb voting on what to have for lunch. > Liberty is a well-armed lamb contesting the vote." > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On Thu, 2006-04-06 at 22:40, Kevin Waterson wrote: > This one time, at band camp, Robert Cummings <[EMAIL PROTECTED]> wrote: > > > I'm gonna go out on a limb here and say WRONG! > > > > Run yourself a benchmark. > > benchmarks can be hazardous, but lets look at them at their most basic level. > By this > I mean how folks use them every day... > > http://www.phpro.org/benchmarks/if-switch-benchmark.html Hazardous is right. That's a terrible benchmark. Could have eliminated hundreds of sources of skew by running the iterations at the command line. It even does an echos *lol*. I ran the following as two shell scripts, 10 times each and averaged the times to get that switch is faster by 0.00122 secs. Big whup, as I said in a previous post, that's very likely due to the precomputation being assigned in userland PHP versus the internal engine. As for which one is best, that's just flamebait -- right up there with preferred braces style, tabs or spaces to indent, top post versus bottom post *teehee*, etc. Personally I prefer if/elseif/else for a moderate number of conditions or if the conditional expressions are very complex. Switch I generally use for a large set of constants. They both have readability pros and cons. Cheers, Rob. *Wheee* ;) -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
At 11:40 PM 4/6/2006, Kevin Waterson wrote: This one time, at band camp, Robert Cummings <[EMAIL PROTECTED]> wrote: > I'm gonna go out on a limb here and say WRONG! > > Run yourself a benchmark. benchmarks can be hazardous, but lets look at them at their most basic level. By this I mean how folks use them every day... http://www.phpro.org/benchmarks/if-switch-benchmark.html Kind regards Kevin -- Actually, a minimal victory. If it can be used, I'd go for the clarity of switch. M. -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.1.385 / Virus Database: 268.3.5/302 - Release Date: 4/5/2006 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
This one time, at band camp, Robert Cummings <[EMAIL PROTECTED]> wrote: > I'm gonna go out on a limb here and say WRONG! > > Run yourself a benchmark. benchmarks can be hazardous, but lets look at them at their most basic level. By this I mean how folks use them every day... http://www.phpro.org/benchmarks/if-switch-benchmark.html Kind regards Kevin -- "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
At 05:56 PM 4/6/2006, John Nichel wrote: I remember IF constructs from BASIC and PASCAL, but no switch statements (somebody correct me if I'm wrong). But what I'm wondering is how in the world did you do conditional checking if there were no switches, and you don't use IF's? Did you not code error handling, different cases based on user input, status of a data stream, etc in all the years prior to something like switch being introduced??? -- John C. Nichel IV Ye gods -- I'm getting old. I had to look this up. Yup - Pascal has case operator of .. statements ... otherwise .. statement end; And you can group cases. Forgotten all that - had to look it up. What I think happens is that we are merrily noodling along in our code, and a condition rears its head. Simple - if .. else. Then things get a bit more complicated, and so we end up with nested if .. elseif .. else constructs. Personally I like the clarity of switch, but don't use it often because I generally try to avoid messy nests of conditions. Cheers - Miles -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.1.385 / Virus Database: 268.3.5/302 - Release Date: 4/5/2006 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On Thursday 06 April 2006 11:20, tedd wrote: > At 1:04 PM -0400 4/6/06, Robert Cummings wrote: > >On Thu, 2006-04-06 at 11:50, tedd wrote: > >> Regardless of speed, I find that switch is much easier to write and > >> debug than if/elseif -- which, regardless of my shortcomings, I never > >> use. > > > >Umm, that you NEVER use elseif I think is strongly coupled with your > >shortcomings :l But I'm not judging, to each his own :| > > > >Cheers, > >Rob. > > Rob: > > Yes NEVER -- as for my shortcomings, they remain as obvious as is my > lack of pretense otherwise. Whereas, my abilities, like most, are not > as obvious. As Will Roger's once said "We're all ignorant, only in > different subjects." > > But regardless of my limitations, I still have never had to use an > if/elseif for anything -- and I wrote my first line of code in 1966. > I don't remember specifically just when if/elseif and switch-like > conditionals first appeared in programming (they haven't always been > there and my old Fortran books have been long stored) but I have one > in front of me that's dated 1976 where it just mentions "The Logical > IF Statement" with no if/else or switch-like statements. > > So, my programming probably predates both conditions -- however -- in > 40 years I have NEVER used an if/elseif control structure by any name > and I always found a way around it -- and one that was usually faster > and with better readability. > > If your strong-comings are better than my shortcomings, then perhaps > you could provide an example of where a switch could not preform what > an if/elseif could -- do you have one? > > My gut feeling is that you can't -- as well as my gut feeling that > when language developers first thought of if/elseif control, they > realized that it was confusing and provided a switch to get around > it. But, then again, maybe I'm wrong -- been there before. :-) > > tedd > -- > --- >- http://sperling.com I'm pretty sure he's ONLY talking about IF/ELSEIF and not IF in general. That's what I got from the message. Correct me if I'm wrong. I ended up deciding to stay with the IF/ELSEIF statements... mostly because I was already done. I did clean up the COUNT()ing though. Always nice when the list helps out :) -- Ray Hauge Programmer/Systems Administrator American Student Loan Services www.americanstudentloan.com 1.800.575.1099 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
tedd wrote: At 1:04 PM -0400 4/6/06, Robert Cummings wrote: On Thu, 2006-04-06 at 11:50, tedd wrote: Regardless of speed, I find that switch is much easier to write and debug than if/elseif -- which, regardless of my shortcomings, I never use. Umm, that you NEVER use elseif I think is strongly coupled with your shortcomings :l But I'm not judging, to each his own :| Cheers, Rob. Rob: Yes NEVER -- as for my shortcomings, they remain as obvious as is my lack of pretense otherwise. Whereas, my abilities, like most, are not as obvious. As Will Roger's once said "We're all ignorant, only in different subjects." But regardless of my limitations, I still have never had to use an if/elseif for anything -- and I wrote my first line of code in 1966. I don't remember specifically just when if/elseif and switch-like conditionals first appeared in programming (they haven't always been there and my old Fortran books have been long stored) but I have one in front of me that's dated 1976 where it just mentions "The Logical IF Statement" with no if/else or switch-like statements. So, my programming probably predates both conditions -- however -- in 40 years I have NEVER used an if/elseif control structure by any name and I always found a way around it -- and one that was usually faster and with better readability. I remember IF constructs from BASIC and PASCAL, but no switch statements (somebody correct me if I'm wrong). But what I'm wondering is how in the world did you do conditional checking if there were no switches, and you don't use IF's? Did you not code error handling, different cases based on user input, status of a data stream, etc in all the years prior to something like switch being introduced??? -- John C. Nichel IV Programmer/System Admin (ÜberGeek) Dot Com Holdings of Buffalo 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On Thu, 2006-04-06 at 14:20, tedd wrote: > At 1:04 PM -0400 4/6/06, Robert Cummings wrote: > > So, my programming probably predates both conditions -- however -- in > 40 years I have NEVER used an if/elseif control structure by any name > and I always found a way around it -- and one that was usually faster > and with better readability. I think I may have read too much into your previous post. Are you saying specifically that you never use the elseif construct or any of the if, elseif, else constructs? If the latter then I think your methodology is somewhat asinine since I'm sure 99% of the programmers out there would view code consisting entirely of switches in place of ifs with a huge grain of WTF :) Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
At 1:04 PM -0400 4/6/06, Robert Cummings wrote: On Thu, 2006-04-06 at 11:50, tedd wrote: Regardless of speed, I find that switch is much easier to write and debug than if/elseif -- which, regardless of my shortcomings, I never use. Umm, that you NEVER use elseif I think is strongly coupled with your shortcomings :l But I'm not judging, to each his own :| Cheers, Rob. Rob: Yes NEVER -- as for my shortcomings, they remain as obvious as is my lack of pretense otherwise. Whereas, my abilities, like most, are not as obvious. As Will Roger's once said "We're all ignorant, only in different subjects." But regardless of my limitations, I still have never had to use an if/elseif for anything -- and I wrote my first line of code in 1966. I don't remember specifically just when if/elseif and switch-like conditionals first appeared in programming (they haven't always been there and my old Fortran books have been long stored) but I have one in front of me that's dated 1976 where it just mentions "The Logical IF Statement" with no if/else or switch-like statements. So, my programming probably predates both conditions -- however -- in 40 years I have NEVER used an if/elseif control structure by any name and I always found a way around it -- and one that was usually faster and with better readability. If your strong-comings are better than my shortcomings, then perhaps you could provide an example of where a switch could not preform what an if/elseif could -- do you have one? My gut feeling is that you can't -- as well as my gut feeling that when language developers first thought of if/elseif control, they realized that it was confusing and provided a switch to get around it. But, then again, maybe I'm wrong -- been there before. :-) tedd -- http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
At 07:48 PM 4/5/2006, Ray Hauge wrote: I have 57 if/elseif/else statements because of all the different criteria. Is it considered better programming practice to use if/elseif/else statements over a switch(true) case (true && false || true || false) syntax? Here are two seemingly contradictory bits of advice: a) Spend more time making your script easy for humans to read than you spend trying to make it machine-friendly. Servers are bloody fast and will process your complex set of If-tests faster than you can click your mouse. Script languages like PHP are purely for the benefit of us humans; they're easier for us to read and write than binary machine language. You might be able to slow down a server with excess disk access and enormous memory demands, but I wouldn't lay awake at night worrying about how to shave a few thousand machine cycles off your code. Instead, think of someone (perhaps yourself) six months or six years from now trying to make sense of your code. Comment prolifically, name your variables and functions sensibly, and use plenty of whitespace. b) Still, there's something to be said for elegance in code-writing, and efficiency as an aesthetic goal even when it's not a practical issue. Since you're using it so much, count your array size once and keep the result in a variable. The computer may or may not be significantly affected but your code will be much cleaner. That said, one thing to keep in mind with regard to conditional processing is that PHP will stop processing a complex conditional if the conclusion is determined early on. Consider this: if ($bCondition == true || count($aThings) > 1) If $bCondition is true, the entire expression will evaluate true, so it's not necessary for the script interpreter to evaluate "count($aThings) > 1". Therefore if you're concerned about processing speed & efficiency you can improve things by putting first the expressions that will evaluate more quickly or will eliminate the most possibilities. Here's a little program that demonstrates this point: if (2 == 2 && say("this will appear")) { say("test 1 = true"); }else{ say("test 1 = false"); } if (1 == 2 && say("this won't appear")) { say("test 2 = true"); }else{ say("test 2 = false"); } function say($msg) { echo "$msg"; return true; } OUTPUT: this will appear test 1 = true test 2 = false The expression "this won't appear" does not appear because PHP stops evaluating the second if-test after determining "1 == 2" to be false. My surmise is that the parser first reduces the syntax to "if (A && B)" so it knows the number of expressions and their Boolean relationships, so that when it begins evaluating the granular expressions in sequence it knows when the overall conclusion is determined. Regards, Paul -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On Thu, 2006-04-06 at 11:50, tedd wrote: > > Regardless of speed, I find that switch is much easier to write and > debug than if/elseif -- which, regardless of my shortcomings, I never > use. Umm, that you NEVER use elseif I think is strongly coupled with your shortcomings :l But I'm not judging, to each his own :| Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On Thu, 2006-04-06 at 09:08, John Wells wrote: > > ...Either way, > > internally, switch behaves like if/elseif/else and generally only > > provides an advantage with respect to code organization or if you want a > > particular condition to drop through to other case blocks. > > > > Considering this is regarding an if/else with ***57*** conditional > checks, wouldn't we be wise in suggesting switch/case if one of its > benefits is code organization and readability? No! Due to the complexity of the conditionals in question nothing but an extra indentation level would be gained by using switch semantics. > Imagine having to come back to this code block in a months time to trace a > bug. > > *shudder* Comments are a tool, and should be used as such. Syntax highlighting can make the experience much easier to swallow since your comments should stand out if written clearly and concisely. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] IF or SWITCH
> Original Message > Subject: Re: [PHP] IF or SWITCH > From: "John Wells" <[EMAIL PROTECTED]> > Date: Thu, April 06, 2006 6:08 am > To: php-general@lists.php.net > > > ...Either way, > > internally, switch behaves like if/elseif/else and generally only > > provides an advantage with respect to code organization or if you want a > > particular condition to drop through to other case blocks. > > > > Considering this is regarding an if/else with ***57*** conditional > checks, wouldn't we be wise in suggesting switch/case if one of its > benefits is code organization and readability? > > Imagine having to come back to this code block in a months time to trace a > bug. > > *shudder* > > I would look very long and hard at how the solution may be simplified, > although it's hard to offer ideas without understanding the business > logic. I'm just hoping for your sake that there is a way. > > Good luck! > > John W > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php Thanks for all the info. I think I am going to take a lot of this information in mind to make it more readable... possibly even use a switch statement, because I know that this logic is going to change, and I WILL have to come back to this. Such a pain to completely cover every possibility under the sun for so many permutations... Ray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
At 7:48 PM -0700 4/5/06, Ray Hauge wrote: Hello World! wait, not coding... (sorry, long night) Okay, I finally finished hashing out all the logic for a very complex set of rules to determine what "type" an application should be set to. I won't bore you with the details of it, but the question is... I have 57 if/elseif/else statements because of all the different criteria. Is it considered better programming practice to use if/elseif/else statements over a switch(true) case (true && false || true || false) syntax? Basically, I'm not too happy with the readability of the code, but I'm afraid that at this point there's not much I can do... code snippet: if($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal){ $retVal = array(TRUE, 'A'); }elseif($numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && $enumFFELP + $numFFELP == $numTotal){ $retVal = array(TRUE, 'A'); }elseif($numFFELP > 0 && $numCONS > 0 && count($FFELP_Lenders) > 1 && $numFFELP + $numCONS == $numTotal){ etc. Any suggestions? Switch. Regardless of speed, I find that switch is much easier to write and debug than if/elseif -- which, regardless of my shortcomings, I never use. I can't stand using elseif's and have never ran into a problem that required their use -- can anyone show me one where a switch would not do just as well, if not better? As for the above, what's wrong with the following? $who_cares = 1; switch ($who_cares) { case $numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal: $retVal = array(TRUE, 'A'); break; case $numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && $enumFFELP + $numFFELP == $numTotal: $retVal = array(TRUE, 'A'); break; ... } OR switch ($numTotal) { case $numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP: $retVal = array(TRUE, 'A'); break; case $numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && $enumFFELP + $numFFELP: $retVal = array(TRUE, 'A'); break; ... } tedd -- http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On 4/6/06, Robert Cummings <[EMAIL PROTECTED]> wrote: > > On Thu, 2006-04-06 at 02:29, Joe Wollard wrote: > > The main perk to using switch over if > > statements is speed (Google can back this up). The reason it's faster is > > because it's simpler by design and is able to jump directly to the case > that > > evaluates to true, whereas an if statement needs to evaluate every > if/elseif > > condition until it finds one that evalutates to true. > > I'm gonna go out on a limb here and say WRONG! > > Run yourself a benchmark. > > Cheers, > Rob. > -- > .. > | InterJinn Application Framework - http://www.interjinn.com | > :: > | An application and templating framework for PHP. Boasting | > | a powerful, scalable system for accessing system services | > | such as forms, properties, sessions, and caches. InterJinn | > | also provides an extremely flexible architecture for | > | creating re-usable components quickly and easily. | > `' > > um. Did you just stick you tounge out at me? (jk) Actually, Rob brings up a good point. switch statements are not always faster. Here's an example from http://www.php.net/manual/en/control-structures.switch.php - note that it says that in certain circumstances switch may be faster than an if. "In a switch statement, the condition is evaluated only once and the result is compared to each case statement. In an elseif statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a switch may be faster." Thanks for making me research that one Rob - I learned something there, and Ray I hope that helps.
Re: [PHP] IF or SWITCH
> ...Either way, > internally, switch behaves like if/elseif/else and generally only > provides an advantage with respect to code organization or if you want a > particular condition to drop through to other case blocks. > Considering this is regarding an if/else with ***57*** conditional checks, wouldn't we be wise in suggesting switch/case if one of its benefits is code organization and readability? Imagine having to come back to this code block in a months time to trace a bug. *shudder* I would look very long and hard at how the solution may be simplified, although it's hard to offer ideas without understanding the business logic. I'm just hoping for your sake that there is a way. Good luck! John W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On Thu, 2006-04-06 at 03:06, Joe Wollard wrote: > > um. Did you just stick you tounge out at me? (jk) Actually, Rob brings Yes, though I forgot the smiley as I'm apt to do :) > up a good point. switch statements are not always faster. Here's an > example from > http://www.php.net/manual/en/control-structures.switch.php - note that > it says that in certain circumstances switch may be faster than an if. > > "In a switch statement, the condition is evaluated only once and the > result is compared to each case statement. In an elseif statement, the > condition is evaluated again. If your condition is more complicated > than a simple compare and/or is in a tight loop, a switch may be > faster." Specifically the evaluation occurs only once at the entry point, but that evaluation will occur for every iteration of an enclosing loop. Given their comments, they are misleading in that the switch will only be faster if you don't precompute the value to compare in your if/elseif/else statements. For example, they are saying switch is faster than the following: Well duh! In practice, what kind of an idiot does that? (ok, ok, don't answer that ;) However, it may be the case, that the overhead of precomputing and assigning to a variable in PHP land is a tiny bit slower than the internal precomputation for switch. Either way, internally, switch behaves like if/elseif/else and generally only provides an advantage with respect to code organization or if you want a particular condition to drop through to other case blocks. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
The main perk to using switch over if statements is speed speed of development and ease of reading. Otherwise, it all ends up the same under the bonnet afaik -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On Thu, 2006-04-06 at 02:29, Joe Wollard wrote: > The main perk to using switch over if > statements is speed (Google can back this up). The reason it's faster is > because it's simpler by design and is able to jump directly to the case that > evaluates to true, whereas an if statement needs to evaluate every if/elseif > condition until it finds one that evalutates to true. I'm gonna go out on a limb here and say WRONG! Run yourself a benchmark. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On 4/5/06, Ray Hauge <[EMAIL PROTECTED]> wrote: > > Hello World! wait, not coding... (sorry, long night) > > Okay, I finally finished hashing out all the logic for a very complex set > of > rules to determine what "type" an application should be set to. I won't > bore > you with the details of it, but the question is... > > I have 57 if/elseif/else statements because of all the different > criteria. Is > it considered better programming practice to use if/elseif/else statements > over a switch(true) case (true && false || true || false) syntax? > > Basically, I'm not too happy with the readability of the code, but I'm > afraid > that at this point there's not much I can do... > > code snippet: > > if($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal){ > $retVal = array(TRUE, 'A'); > }elseif($numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && > $enumFFELP + $numFFELP == $numTotal){ > $retVal = array(TRUE, 'A'); > }elseif($numFFELP > 0 && $numCONS > 0 && count($FFELP_Lenders) > 1 && > $numFFELP + $numCONS == $numTotal){ > etc. > > Any suggestions? > > Thanks, > -- > Ray Hauge > Programmer/Systems Administrator > American Student Loan Services > www.americanstudentloan.com > 1.800.575.1099 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > Without seeing more code it's hard to say which would be better for you to use, but based on the snippet you've provided it looks like an if block is the only thing that will work for you. The main perk to using switch over if statements is speed (Google can back this up). The reason it's faster is because it's simpler by design and is able to jump directly to the case that evaluates to true, whereas an if statement needs to evaluate every if/elseif condition until it finds one that evalutates to true. In your example you're passing the $FFELP_Lenders variable through count() multiple times. One way to speed things up would be to cache the results of count($FFELP_Lenders) in a separate var so that the entire $FFELP_Lenders array isn't being counted potentially 50+ times - unless of course you're manipulating it later in the code. You can also clean up this code a bit by removing other redundancies. In the example below we're only evaluating $numFFELP one time. If it is not greater than 1 then everything inside that block that depends on $numFFELPS being > 1 will be skipped - getting you one step closer to the speed of a switch statement. // Cache the number of elements in the // $FFELPS_Lenders array $numFFELP_Lenders = count($FFELP_Lenders); if($numFFELP > 1) { // At this point we know $numFFELP is greater than one if ($numFFELP_Lenders > 1 && $numFFELP == $numTotal) $retVal = array(TRUE, 'A'); }else { // From here on out we know that $numFFELP // is greater than 0. If it's not this block of code // will be completely ignored. if($enumFFELP > 0 && $numFFELP_Lenders > 1 && $enumFFELP + $numFFELP == $numTotal){ $retVal = array(TRUE, 'A'); }elseif($numCONS > 0 && $numFFELP_Lenders > 1 && $numFFELP + $numCONS == $numTotal){ // Rob says "Wh!"!11!!!11one1! } } It's been a long night for me as well so I hope this makes as much sense to me in the morning as I think I does right now ;-)
RE: [PHP] IF or SWITCH
> Original Message > Subject: Re: [PHP] IF or SWITCH > From: Chris <[EMAIL PROTECTED]> > Date: Wed, April 05, 2006 8:32 pm > To: Ray Hauge <[EMAIL PROTECTED]> > Cc: PHP-General > > Ray Hauge wrote: > > Hello World! wait, not coding... (sorry, long night) > > > > Okay, I finally finished hashing out all the logic for a very complex set > > of > > rules to determine what "type" an application should be set to. I won't > > bore > > you with the details of it, but the question is... > > > > I have 57 if/elseif/else statements because of all the different criteria. > > Is > > it considered better programming practice to use if/elseif/else statements > > over a switch(true) case (true && false || true || false) syntax? > > > > Basically, I'm not too happy with the readability of the code, but I'm > > afraid > > that at this point there's not much I can do... > > > > code snippet: > > > > if($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal){ > > $retVal = array(TRUE, 'A'); > > }elseif($numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && > > $enumFFELP + $numFFELP == $numTotal){ > > $retVal = array(TRUE, 'A'); > > }elseif($numFFELP > 0 && $numCONS > 0 && count($FFELP_Lenders) > 1 && > > $numFFELP + $numCONS == $numTotal){ > > etc. > > Are you in a function? Maybe it'll be clearer/easier to follow if you > return when you find the right condition: > > if ($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal) { >return array(TRUE, 'A'); > } > > if (. > > PS - count($array) does a count every time, so depending on how large > you expect this array to get, it could be quicker (processing time) to: > > $count_lenders = count($FFELP_Lenders); > > if ($count_lenders > 1) > > -- > Postgresql & php tutorials > http://www.designmagick.com/ I should change that count out. Thanks for the tip. This is actually in a function, but I prefer to only have one location of returning. I think it makes it easier to debug. Ray -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
Chris wrote: Ray Hauge wrote: Hello World! wait, not coding... (sorry, long night) ... if ($count_lenders > 1) I would take it even further and setup a load of variables that hold the boolean states that you what to check in the 50 odd if/else statements... something like: $ffelpEQtotal= ($numFFELP == $numTotal); $ffelptotEQtotal = (($enumFFELP + $numFFELP) == $numTotal); $countlenders= count($FFELP_Lenders); $lendersGTone= $countlenders > 1; I would do this purely for readability in the if/else statements (i.e. so I could keep them as short as possible.) this is assuming the boolean states you are checking are quite repetitive (although the combination changes each time) and I would personally probably write it up as a switch statement because I feel it looks 'nicer' and it's more compact, thats down to personal opinion of course. something like: switch (true) { case $ffelpEQtotal && $lendersGTone: return array(true, 'A'); case $ffelptotEQtotal: return array(true, 'A'); // weee! etc :-) } some people consider this use of switch to be a complete misuse - be careful with things that autocast cast to true - know what you're doing :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
Ray Hauge wrote: Hello World! wait, not coding... (sorry, long night) Okay, I finally finished hashing out all the logic for a very complex set of rules to determine what "type" an application should be set to. I won't bore you with the details of it, but the question is... I have 57 if/elseif/else statements because of all the different criteria. Is it considered better programming practice to use if/elseif/else statements over a switch(true) case (true && false || true || false) syntax? Basically, I'm not too happy with the readability of the code, but I'm afraid that at this point there's not much I can do... code snippet: if($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal){ $retVal = array(TRUE, 'A'); }elseif($numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && $enumFFELP + $numFFELP == $numTotal){ $retVal = array(TRUE, 'A'); }elseif($numFFELP > 0 && $numCONS > 0 && count($FFELP_Lenders) > 1 && $numFFELP + $numCONS == $numTotal){ etc. Are you in a function? Maybe it'll be clearer/easier to follow if you return when you find the right condition: if ($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal) { return array(TRUE, 'A'); } if (. PS - count($array) does a count every time, so depending on how large you expect this array to get, it could be quicker (processing time) to: $count_lenders = count($FFELP_Lenders); if ($count_lenders > 1) -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On Wednesday 05 April 2006 20:11, Robert Cummings wrote: > Well I'd use if/elseif/else but I don't format like you... > > if( $numFFELP > 1 > && > count( $FFELP_Lenders ) > 1 > && > $numFFELP == $numTotal ) > { > $retVal = array( TRUE, 'A' ); > } > else > if( $numFFELP > 0 > && > $enumFFELP > 0 > && > count( $FFELP_Lenders ) > 1 > && > ($enumFFELP + $numFFELP) == $numTotal ) > { > $retVal = array( TRUE, 'A' ); > } > else > if( $numFFELP > 0 > && > $numCONS > 0 > && > count( $FFELP_Lenders ) > 1 > && > ($numFFELP + $numCONS) == $numTotal ) > { > // Wh! > } > ?> > > Yeah it's vertically sprawling, yeah it looks tedious to type, but damn > is it clear *lol*. Once you get into a habit it becomes second nature. > > Cheers, > Rob. Thanks for the input. I especially like the Wheee part. Maybe I'll add that in there... it's been a long day *lol* -- Ray Hauge Programmer/Systems Administrator American Student Loan Services www.americanstudentloan.com 1.800.575.1099 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] IF or SWITCH
On Wed, 2006-04-05 at 22:48, Ray Hauge wrote: > Hello World! wait, not coding... (sorry, long night) > > Okay, I finally finished hashing out all the logic for a very complex set of > rules to determine what "type" an application should be set to. I won't bore > you with the details of it, but the question is... > > I have 57 if/elseif/else statements because of all the different criteria. > Is > it considered better programming practice to use if/elseif/else statements > over a switch(true) case (true && false || true || false) syntax? > > Basically, I'm not too happy with the readability of the code, but I'm afraid > that at this point there's not much I can do... > > code snippet: > > if($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal){ > $retVal = array(TRUE, 'A'); > }elseif($numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && > $enumFFELP + $numFFELP == $numTotal){ > $retVal = array(TRUE, 'A'); > }elseif($numFFELP > 0 && $numCONS > 0 && count($FFELP_Lenders) > 1 && > $numFFELP + $numCONS == $numTotal){ > etc. Well I'd use if/elseif/else but I don't format like you... 1 && count( $FFELP_Lenders ) > 1 && $numFFELP == $numTotal ) { $retVal = array( TRUE, 'A' ); } else if( $numFFELP > 0 && $enumFFELP > 0 && count( $FFELP_Lenders ) > 1 && ($enumFFELP + $numFFELP) == $numTotal ) { $retVal = array( TRUE, 'A' ); } else if( $numFFELP > 0 && $numCONS > 0 && count( $FFELP_Lenders ) > 1 && ($numFFELP + $numCONS) == $numTotal ) { // Wh! } ?> Yeah it's vertically sprawling, yeah it looks tedious to type, but damn is it clear *lol*. Once you get into a habit it becomes second nature. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] IF or SWITCH
Hello World! wait, not coding... (sorry, long night) Okay, I finally finished hashing out all the logic for a very complex set of rules to determine what "type" an application should be set to. I won't bore you with the details of it, but the question is... I have 57 if/elseif/else statements because of all the different criteria. Is it considered better programming practice to use if/elseif/else statements over a switch(true) case (true && false || true || false) syntax? Basically, I'm not too happy with the readability of the code, but I'm afraid that at this point there's not much I can do... code snippet: if($numFFELP > 1 && count($FFELP_Lenders) > 1 && $numFFELP == $numTotal){ $retVal = array(TRUE, 'A'); }elseif($numFFELP > 0 && $enumFFELP > 0 && count($FFELP_Lenders) > 1 && $enumFFELP + $numFFELP == $numTotal){ $retVal = array(TRUE, 'A'); }elseif($numFFELP > 0 && $numCONS > 0 && count($FFELP_Lenders) > 1 && $numFFELP + $numCONS == $numTotal){ etc. Any suggestions? Thanks, -- Ray Hauge Programmer/Systems Administrator American Student Loan Services www.americanstudentloan.com 1.800.575.1099 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php