views:

111

answers:

5
+1  Q: 

Question on ifs

Hi,

I have the following code:

     boolean Short = x();
    boolean Long = y();       
    boolean Longer = z();

    if (Short )
        return "abc";

    if (Long)
        return "def";

    if (Longer) 
        return "ghi";        

The three bool methods (x, y, z) all do maths on value. I need an additional if statement to return the number from if the above 3 ifs are not evaluated to true. How could this be done and are there any redundant ifs? Also, what do I need to know about precedence with ifs? My name says "dotnet" but I am equally a programmer in Java (I've spent time trying to pick it up as much as .NET).

Thanks

+5  A: 

It's fine, but you do not need another if:

boolean Short = x();
boolean Long = y();       
boolean Longer = z();

if (Short )
    return "abc";

if (Long)
    return "def";

if (Longer) 
    return "ghi"; 

return "none of the above";

The if statements will evaluate in order, once one of them is true, the return statement will end execution within the method, so nothing after it will be evaluated.

If none of them are true, the last return will end execution.

You could substitute the variables with the calls themselves if you do not need to evaluate y() and z() when x() is true, nor z() if y() is true:

if (x())
    return "abc";

if (y())
    return "def";

if (z()) 
    return "ghi"; 

return "none of the above";
Oded
+1  A: 

Just put:

boolean Short = x();
boolean Long = y();       
boolean Longer = z();

if (Short )
    return "abc";

if (Long)
    return "def";

if (Longer) 
    return "ghi";

return "xyz";
Aaron
A: 

Hm.. Probably this

if (x())
    return "abc";

if (y())
    return "def";

if (z()) 
    return "ghi"; 
return "smt"; 
Stas
This is not the same if `y()` or `z()` have side effects and need to run regardless of the value of `x()`.
Oded
@Oded : Yep. But, it's bad if such functions have any side effects, imho.
Stas
@Stas - me experience is to expect bad things in code ;)
Oded
@Oded : And I'm still optimist ;)
Stas
A: 

In order to return a value for all three being false, simply do this :

if(!Long && !Short && !Longer)
   return "Whatever you are returning"
Jas
Downvoted because this could potentially change the method's behavior. In the original version, if both Short and Long are true, then "abc" is returned. In your version, if both Short and Long are trye, "def" is returned. You would need to turn this into an if-else-if chain to make it work.
Mike Daniels
Yep you had a point there. It seems better this way.
Jas
+1  A: 

There are no redundant if's. You just need an extra return in case none of the three conditions evaluate to true. You can also add else clauses if you want.

boolean Short = x();
boolean Long = y();       
boolean Longer = z();

if (Short )
    return "abc";
else if (Long)
    return "def";
else if (Longer) 
    return "ghi";
else
    return "something else";

Or, if the methods x(), y(), z() have no side effects:

if (x())
    return "abc";
else if (y())
    return "def";
else if (z()) 
    return "ghi";
else
    return "something else";

Also, you can use the ? operator for a more compact, but perhaps more cryptic syntax:

return x()? "abc" :
       y()? "def" :
       z()? "ghi" :
       "something else";

But ifs are probably more readable than this :-)

Grodriguez
What is the definition of a side effect?
dotnetdev
A function or method has a side effect if, in addition to returning a value, also modifies its environment in an observable way; for example, it could modify some external variable. If methods `x()`, `y()` and `z()` had side effects, then the 2nd and 3rd code snippets in my answer would not necessarily be equivalent to the first one, because in the first one all three methods would always be evaluated (thus always producing the corresponding side effects), while in the 2nd and 3rd code snippets, some methods might not be evaluated (thus the corresponding side effects would not be produced).
Grodriguez