-1

I'm trying to perform actions on my object, but only do so if certain things are true. I have several methods which is consider "action" methods, they do some action that attempts to modify the object and returns true/false if that action can be done.

Example 1

Thing thing = new Thing();
if (thing.changeSomething()){
    if (thing.shouldDoSomething()){
        //do more things
    }
}

I know about compound boolean expressions like to check if a number in a valid range of values

if(number>0 && number<=10)
    //number is valid

But haven't really done much when the sub-expressions are method calls

Example 2

if ( thing.changeSomething() && (thing.shouldDoSomething() ){
    //do more things
}

Is Example 2 the same as Example 1?
Will Example 2 call the shouldDoSomething() method? Because I don't want this to happen because sometimes shouldDoSomething() actually has other implications & changes other aspects of the object.

  • So which language are you talking about? You tagged both java and c#? I don't know java enough to say if it's the same, but in C# the expression `b` in `if (a && b)` is only evaluated if `a` is `true`. So `thing.shouldDoSomething()` in Example 2 would only be called if `thing.changeSomething()` return `true`. – René Vogt Jun 09 '17 at 14:22
  • I'm voting to close this question as off-topic because of fraud – adjan Jun 09 '17 at 14:24
  • If you intend to perform conditional logic based on the state of your object, you generally want to expose that state in a way that doesn't change the state first. A method that determines whether something should or should not be performed should not have to change the state of its context object in order to determine if the action needs to take place. I've seen many programmers get bit by this in code they've inherited. This is an antipattern that should be avoided. – S.C. Jun 09 '17 at 14:24
  • 2
    Possible duplicate of [Does Java evaluate remaining conditions after boolean result is known?](https://stackoverflow.com/questions/6352139/does-java-evaluate-remaining-conditions-after-boolean-result-is-known) – OH GOD SPIDERS Jun 09 '17 at 14:28

3 Answers3

1

Calling

if (thing.changeSomething()){
    if (thing.shouldDoSomething()){

is esentially equivalent to

if (thing.changeSomething() && thing.shouldDoSomething()){

I guess they are translated to the exact same IL.

However as mentioned in the comments the second method is only executed if the first one evaluates to true. If the first operand of an &&-operator is already false there´s no need to execute the second also, so your shouldDoSomething-method isn´t executed if changeSomething allready returned false.

Btw. this applies to both Java and C#.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

The simple answer and what it comes down to is, it depends on if the thing.changeSomething() method returns a boolean. It is going to create problems if it does not. It is very possible to define this method to do something and return true after it does what it was supposed to do. In which case the second example would work.

If it does not return boolean you should see an error or it might not work to your liking.

It should call the method when to do it that way in example 2. What I would recommend because you don't want that is to create getter() and checker() methods that you use to get info on the object without having to change it. This can also be done with data fields, depends on class structure.

Hope this is more digestible and helps!

RostSunshine
  • 216
  • 4
  • 14
0

In Java, the logical AND operator && is a short circuit operator, meaning the right side is not evaluated if the result of the expression can be determined solely from the left operand. Specifically, if the left side evaluates to false, the whole expression is false and the right side does not need to be evaluated to determine that.

In the case of your function calls:

if ( thing.changeSomething() && (thing.shouldDoSomething() ){
    //do more things
}

If thing.changeSomething() returns false then thing.shouldDoSomething() will not be called since the expression evaluates to false regardless of what this function may return. So yes, the above is equivalent to:

if ( thing.changeSomething() ) {
    if (thing.shouldDoSomething() ){
        //do more things
    }
}
dbush
  • 205,898
  • 23
  • 218
  • 273