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?