vendredi 9 juin 2017

Do if statements that call methods on an object modify the object?

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.

Aucun commentaire:

Enregistrer un commentaire