patternjavaModerate
Check and clear last value
Viewed 0 times
lastvalueclearandcheck
Problem
I have a method like this:
This works, but is there a “smarter” way to code 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:
This should be two separate methods so as to not violate SRP and enable checking of that status without resetting it.
Don't introduce quantum behaviour in your code, where observing a state changes it!
- 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.