Eg.
boolean isCurrent = false;
What do you name its getter and setter?
Eg.
boolean isCurrent = false;
What do you name its getter and setter?
Suppose you have
boolean active;
Accessors method would be
public boolean isActive(){return this.active;}
public void setActive(boolean active){this.active = active;}
See Also
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;
}
There is a markable point between setter/getter method of the data type Boolean and boolean in side a class ( for pojo/entity).
Example:
(a) if property is defines as Boolean
private Boolean check;
the setter/getter method
public Boolean getCheck() { // getXXX()
return check;
}
public void setCheck(Boolean check) {
this.check = check;
}
(b) if property is defines as boolean
private boolean check;
the setter/getter method
public boolean isCheck() { // isXXX()
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
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.
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.
It is highly recommended to use an adjective to name a boolean field. If you generate getter and setter using IntelliJ, you will find out that the getter is isCurrent()
for both of boolean fields current
and isCurrent
.
We can take a look at IntelliJ community source code, its test data shows that no matter whether your boolean field name starts with is
or not, the name of getter starts with is
.
class Getter {
boolean foo;
boolean isBar;
boolean hasBaz;
@java.lang.SuppressWarnings("all")
public boolean isFoo() {
return this.foo;
}
@java.lang.SuppressWarnings("all")
public boolean isBar() {
return this.isBar;
}
@java.lang.SuppressWarnings("all")
public boolean isHasBaz() {
return this.hasBaz;
}
}
It will be very confusing when you want to call the getter, when you boolean field name starts with is
. Besides, when your colleagues want to get the value of a boolean field you defined, they will only know the getter' s name instead of the field' s name. In that case, the prefix is
is not necessary.
Here is another example, when I retrieve data from database to instantiate a object of Employee, the value of isRetired
is always false
. Because Java does not find an appropriate setter, the value of a boolean field is always default value, say false
, which is not expected.
class Employee{
private int age;
private boolean isRetired;
...
public boolean setRetired(boolean isRetired){
this.isRetired = isRetired;
}
}
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()
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.