Jump to content

Argument branching vs stand-alone control structures


chibineku
 Share

Recommended Posts

Even using programmer's text editors with syntax highlighting and bracket-matching (placing the cursor adjacent to a bracket will highlight its partner, if it has one), I sometimes find I get lost when I'm trying to use several else-ifs or if the blocks of code to execute inside each one are long and push matching braces beyond the end of the page. So I sometimes add stand alone control structures inside an if statement. For example:instead of

if(x) {if(y) {} else if(z) {}}

I might write:

if(x) {if(y) {}} else if(z) {}

Obviously this only works if you intend to either return inside the if(y) condition or exit the script (sending a location header, for example).I find this easier sometimes, especially if I'm returning to old code to add a new condition which will cause script termination. Is this considered improper or does it not matter really?

Edited by chibineku
Link to comment
Share on other sites

That's what indenting is for! Anyway, both of those condition trees have different uses, I don't quite understand what the question is... are you thinking of assertions?

Link to comment
Share on other sites

Bad code example. Here's a real world example.I recently wanted to change the way my product search result page worked. I was checking the number of rows returned from a mysql select query and, if it was 0, displaying a message that there were no matches for the search term(s), and if there were more than 0 rows, returning a list of thumbnails, some details and a link to the full details page of each product.I decided it would be neater if, in the event of one result, users were automatically redirected to the full details of the product. Instead of doing:

if(mysqli_num_rows($res) == 0) {} else if(mysqli_num_rows($res) ==1) {} else if(mysqli_num_rows($res) > 1) {}

I just did this:

if(mysqli_num_rows($res) == 0) {} else if(mysqli_num_rows($res) > 0) {if(mysqli_num_rows($res) == 1) {//redirect + exit}//otherwise list results}

I know that's what indentation is for, but I'm a pretty sloppy coder - a bad habit, I know, but its not my only one and it won't be my last.

Link to comment
Share on other sites

Think about the number of checks you'll have to do. Best case and worst case scenarios.In either of your samples, best case is 1 check - when there are no records. Worst case in both samples is both having 1 record or having multiple records - 3 checks. So, in this reduced sample at least, there's no difference in the way you'd write the condition.BTW, it's also a good practice to store results from functions in a variable to save some time on repeated processing. Alternatively, just use a switch.Also, I think the best case is to target cases as a best, better and worst case, so that at least sometimes you do fewer checks. Like for example, 0 being best, 1 being better, and more than one being worst.Here's, I think, a more optimized version of your code:

switch(mysqli_num_rows($res)) {case 0:  //no records  break;case 1:  //redirect + exit  break;default:  //more than one record}

P.S. If you have a "comparrison" feature in this product search, consider having another case between 1 and default, where a comparrison between the two products is automatically shown.

Link to comment
Share on other sites

Both those options are good but honestly I say you try to get into the habit of indenting. It helps a great deal when you get to debugging and at the same time it's a lot simpler for others that look at your code to understand it from first glance.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...