HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Check and clear last value

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lastvalueclearandcheck

Problem

I have a method like this:

public boolean wasUserInactive() {
    boolean result = userInactive;
    userInactive = false;
    return result;
}


This works, but is there a “smarter” way to code this?

Solution

This method satisfies two concerns:

  • mark users as active



  • return the last status of inactivity



This should be two separate methods so as to not violate SRP and enable checking of that status without resetting it.

wasUserInactive should be isUserInactive and just return userInactive;. Additionally there should be a method resetting the inactive status to false.

Don't introduce quantum behaviour in your code, where observing a state changes it!

Context

StackExchange Code Review Q#108138, answer score: 12

Revisions (0)

No revisions yet.