Eg.
boolean isCurrent = false;
What do you name its getter and setter?
This question is related to
java
coding-style
naming-conventions
javabeans
I believe it would be:
void setCurrent(boolean current)
boolean isCurrent()
As a setter, how about:
// setter
public void beCurrent(boolean X) {
this.isCurrent = X;
}
or
// setter
public void makeCurrent(boolean X) {
this.isCurrent = X;
}
I'm not sure if these naming make sense to native English speakers.
Maybe it is time to start revising this answer? Personally I would vote for setActive()
and unsetActive()
(alternatives can be setUnActive()
, notActive()
, disable()
, etc. depending on context) since "setActive" implies you activate it at all times, which you don't. It's kind of counter intuitive to say "setActive" but actually remove the active state.
Another problem is, you can can not listen to specifically a SetActive event in a CQRS way, you would need to listen to a 'setActiveEvent' and determine inside that listener wether is was actually set active or not. Or of course determine which event to call when calling setActive()
but that then goes against the Separation of Concerns principle.
A good read on this is the FlagArgument article by Martin Fowler: http://martinfowler.com/bliki/FlagArgument.html
However, I come from a PHP background and see this trend being adopted more and more. Not sure how much this lives with Java development.
http://geosoft.no/development/javastyle.html#Specific
is
prefix should be used for boolean variables and methods.
isSet
,isVisible
,isFinished
,isFound
,isOpen
This is the naming convention for boolean methods and variables used by Sun for the Java core packages. Using the is prefix solves a common problem of choosing bad boolean names like status or flag. isStatus or isFlag simply doesn't fit, and the programmer is forced to chose more meaningful names.
Setter methods for boolean variables must have set prefix as in:
void setFound(boolean isFound);
There are a few alternatives to the is prefix that fits better in some situations. These are has, can and should prefixes:
boolean hasLicense(); boolean canEvaluate(); boolean shouldAbort = false;
For a field named isCurrent
, the correct getter / setter naming is setCurrent()
/ isCurrent()
(at least that's what Eclipse thinks), which is highly confusing and can be traced back to the main problem:
Your field should not be called isCurrent
in the first place. Is is a verb and verbs are inappropriate to represent an Object's state. Use an adjective instead, and suddenly your getter / setter names will make more sense:
private boolean current;
public boolean isCurrent(){
return current;
}
public void setCurrent(final boolean current){
this.current = current;
}
It should just be get{varname} like every other getter. Changing it to "is" doesn't stop bad variable names, it just makes another unnecessary rule.
Consider program generated code, or reflection derivations.
It's a non-useful convention that should be dropped at the first available opportunity.
private boolean current;
public void setCurrent(boolean current){
this.current=current;
}
public boolean hasCurrent(){
return this.current;
}
Setter: public void setCurrent(boolean val)
Getter: public boolean getCurrent()
For booleans you can also use
public boolean isCurrent()
Source: Stackoverflow.com