This is purely a design question and the example is simple to illustrate what I am asking and there are too many permutations of more complex code to provide examples that would cover the topic. I am not referring specifically to this example, just using the example to illustrate what I am referring to.
In an if statement, is there a preferred way to order conditions, in terms of coding convention and, more importantly, efficiency?
For such a simple example, efficiency will not be an issue, but I am not asking for a code review per se, but an understanding of the concept.
Two alternatives:
1.
public double myStuff(int i)
{
// check for an invalid number of tourists
if (i > 0)
{
// do something here
return doubleSomething;
} else
{
return Double.NaN;
}
}
2.
public double myStuff(int i)
{
if (i <= 0)
{
return Double.NaN;
} else
{
{
// do something here
return doubleSomething;
}
If the if statement was to become more complex, and possibly nested, in what order should conditions be addressed?
NO
The java compiler would compiler both of them into almost similar byte codes. – Prateek Nov 16 '13 at 0:29NO
doesn't make sense. – user104150 Nov 17 '13 at 1:54In an if statement.. is there a preferred way to order conditions, in terms of coding convention and, more importantly, efficiency.
I think in this contextNo
does make sense. – Prateek Nov 17 '13 at 2:02If the if statement was to become more complex, and possibly nested, in what order should conditions be addressed?
The rest is the blurb leading up to the question (hence no question mark.) anyway I'm not here to argue that.. if the answer is so simple, why not post an answer, rather than a glib comment – user104150 Nov 17 '13 at 2:05