user2891462
user2891462

Reputation: 3333

Standard name for method to check and set a variable

I am aware that the 'standard' name for methods to check and to change the value of a boolean variable X are getX(), isX() and setX().

Is there a standard name for a method which returns the value and changes it after returning it but only the first time (i.e. I cannot use toggleX() because it will only toggle the first time)? I want to have a method which returns true only the first time it is called, so something like:

public boolean isFirstExecution() {
    if (mIsFirstExecution) {
        mIsFirstExecution = false;
        return true;
    } else {
        return mIsFirstExecution;
    }
}

The problem is that I think it might be confusing to have a method called isX() changing the value of a variable.

Upvotes: 0

Views: 1378

Answers (2)

Roshan Mathews
Roshan Mathews

Reputation: 5898

The problem is that you're trying to have that method do two things – check and set. Instead you can have two methods, isFirstExecution() (that is a check mIsFirstExecution) and executedFirstTime() (that will turn off mIsFirstExecution.)

All of this seems too "enterprisey" though, I would suggest just exposing mIsFirstExecution directly to callers, and doing away with the methods.

Upvotes: 0

jooShin
jooShin

Reputation: 140

I do not think there's specific name for the function that you are intend to make. Just specify the purpose of function when you name it. Not too long but specific enough so that others also can recognize when they see your code.

Upvotes: 1

Related Questions