views:

56

answers:

3

In a simple if-else case (where the denominator of a fraction will need to be checked for a value of zero for each entry), does a correct arrangement of the statements even exist?

For example:

    if (!(denominator == 0))
    {
        quotient = numerator / denominator;
        System.out.println(numerator + " / " + denominator + " = " + quotient);
    }
    else
    {
        System.out.println("Division by zero is not allowed.");
    }

or

    if (denominator == 0)
    {
        System.out.println("Division by zero is not allowed.");
    }
    else
    {
        quotient = numerator / denominator;
        System.out.println(numerator + " / " + denominator + " = " + quotient);
    }

I know this is a trivial example, but I am curious if there is any benefit to giving consideration to the ordering of the if-else statements (in a case such as above). For less trivial testing of input/values, I can understand the consensus answer put forth here: Best Practice on IF/ELSE Statement Order.

Apologies in advance if this is a bad question (or one with what should have an obvious answer). My intent is to increase the general efficiency of my coding. Although, I doubt that there is any significant difference between the two examples that I have given. My newbie analysis indicates that the denominator will be tested for zero either way therefore negating any benefit of testing for the "normal case". Please correct/enlighten me if I am wrong.

TIA

+2  A: 

Steve McConnell has some advice in Code Complete. He suggest that the non-abnormal or most frequently used branch of the if statement should appear first. (Your first code example.)

With regards to performance there should be no difference.

Martin Liversage
Thank you. I suspected as much (first example / no performance difference).I need to pick up a copy of Code Complete. Seems like it has been mentioned to me one too many times lately (due to my tendency to have a lot of 'technical' questions).
EtherealMonkey
Also, I asked this question because the first code example is the answer that I provided for a homework question. But, a follow-up question seemed to be worded in such a way that it appeared that the second example was being expected.I felt that the first case was more correct, but I am learning Java (having taught myself C# and only previously having formal training in C).Thanks for the sanity check.
EtherealMonkey
A: 

In the end, it's the compiler's job to order this sort of thing for maximal performance. When it comes to a detail this trivial, your primary concern should nearly always be for simple and readable code. So the second example would get my vote because there's (marginally) less to it.

Reinderien
Thank you for this: "simple and readable code."I do try... I will keep this in mind though as I work my way through this course.You may be on to something as I have stated in the comment above that I *suspected* that example two was preferred, but I chose to submit the first example because it felt "more natural" to me.Only having been given the next assignment did I question my original code (because I was asked to re-use the code from my first submission in a way that was not possible using the code in the first block above).
EtherealMonkey
+1  A: 

Another answer here mentions Code Complete (a great book) as an example of having the most normal case first. I agree with this completely. However, as your behaviour isn't "if(normal) doSomething (otherwise rare case) doSomethingRarer" I don't think this applies. Insted McConnell's advice of exit early seems appropriate:

/* Probably should be throw new Exception(".."); */
if (denominator == 0) 
    return "Division by zero is not allowed."; 


quotient = numerator / denominator; 
return(numerator + " / " + denominator + " = " + quotient);
Graphain
Yes, I most definitely agree with throwing here. Or at least re-throwing. But, we aren't that far along in this course yet. I would *definitely* have done this if given a program and told to make it work...So, +1 on the Code Complete? I happen to know where a local copy is at. Now, I must convince the wife that this is needed ;-)
EtherealMonkey
Also, I am inclined to promote your answer. Especially since you touch on "early exit" as I am sure is a best practice. However, (to be strictly pedantic) my question was regarding ex1 or ex2. While I agree that your ex (ex3?) is more correct, it is not an available option to me at this point. But, I do appreciate the thorough answer.
EtherealMonkey
Glad to see you're thinking about these things and definitely +1 on the code complete. I might even give it a re-read. The Pragmatic Programmer is another good, but shorter and less deep, book on program design (not that CC is particularly deep).
Graphain