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?
Ordering if conditions for efficiency and clean code
java;programming practices;optimization;clean code;programming logic
When it comes to large if blocks, optimization isn't really an issue. The compiler will generate pretty much the same result no matter how you organize them, assuming that the conditions are equally easy to calculate.One technique I like to use is to use guard statements to check for invalid input right away, at the top of the method body. This way, the rest of the method can be formatted with the assumption that the arguments are already valid.public double myStuff(int i, SomeOtherArgument widget) {    // Perform any simple checks for invalid data right away and    // fail fast if something smells bad.    if (i <= 0)        return Double.NaN;    if (widget == null)        throw new IllegalArgumentException();    // If the stuff you've done above this line is trivial, there's no reason    // to wrap the rest of the method body in a big else block. It's just noise.    return doABunchOfWork();}