PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
VladSun wrote:What's the importance of the N (in this case 50) period? Is it a must for your design or not?
There is no significance to the conditions I chose other than the number of conditions is greater than the number of allowed if statements. I chose the ranges I did because they seemed realistic and to make people think there might be some hidden relationship. Maybe there is, but not by design. The "pattern that I think is somewhat clever" is related to changing a variable based on a where it fits into a group of unequal conditions.
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 5:30 pm, edited 1 time in total.
I found it's called "short-circuit evaluation". I remember, Turbo Pascal has a special compiler flag used to enable or disable short-circuit evaluation of the expressions in an if statement, though I think it's always enabled in modern languages.
I vote for the prize going to VladSun for either the binary logic or the lookup table. Both sweet!
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Maybe I'm stating the obvious but it took me a minute to figure out. When the array is populated whichever expression gets evaluated to true gets type cast from a boolean to an integer (one), which is then accessed in the return
Very intriguing stuff. It seems like it'd be way over my head to ever work on such constructs. I would never intentionally use this technique personally I'd rather see a couple ugly if statements tucked away with code collapse.
Only interest I'd have is if I had to maintain code that abused the technique lol
There are obvious disadvantages when using such "look up" tables - every "address" and it's corresponding "value" MUST be evaluated before the lookup action takes place.
Last edited by VladSun on Sun Mar 07, 2010 4:32 pm, edited 2 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
pytrin wrote:I've begun to use this technique to replace tedious switch statements. I'm really liking it so far
I think learning the stuff is fun & important because we'll all have to debug code like this.
But I'm just wondering what potential advantages you see in it that would make you actually rewrite a switch statement to this technique? Do you have a boss that reprimands you for using more than one curly brace for a conditional? Do you find the stuff "readable"? (FYI I do not). It is more "mental juggling" in my opinion
I find it much more readable, to be honest. When you have a set of predetermined values that depend on ranges of a variable, it is much easier to see all the values in an array format than in otherwise a much longer switch statement. For example, compare the following pieces of code which both return a screen-size range for laptops depending on a given value -
The original:
Yes that does look cleaner (if you are familiar with the concept). Thanks for clarifying.
Also I would advise if using in a real system, would advise a comment right before the return statement (something that at least provides a term someone can type into google to learn about this pattern). Or maybe not a comment but using the word "lookup" in the function name so people know what the heck it does
and instead of hard coding the integer "one", making a constant like self::ScreenSize, and self::DefaultSize then I think I would be hard pressed to come up with any more gripes