For a boolean field, what is the naming convention for its getter/setter?

JavaCoding StyleNaming ConventionsJavabeans

Java Problem Overview


Eg.

boolean isCurrent = false;

What do you name its getter and setter?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

http://geosoft.no/development/javastyle.html#Specific

> 14. 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;

Solution 3 - Java

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;
}

Solution 4 - Java

I believe it would be:

void setCurrent(boolean current)
boolean isCurrent()

Solution 5 - Java

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.

Solution 6 - Java

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.

Solution 7 - Java

There is a markable point between setter/getter method of the data type Boolean and boolean in side a class ( for pojo/entity).

  • For both Boolean and boolean the setter method should be setXXX() but getter method would be getXXX() and isXXX() respectively

Example:

(a) if property is defines as Boolean

private Boolean active;

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;
	}

Solution 8 - Java

private boolean current;

public void setCurrent(boolean current){
    this.current=current;
}

public boolean hasCurrent(){
    return this.current;
}

Solution 9 - Java

Setter: public void setCurrent(boolean val)
Getter: public boolean getCurrent()

For booleans you can also use

public boolean isCurrent()

Solution 10 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser496949View Question on Stackoverflow
Solution 1 - JavajmjView Answer on Stackoverflow
Solution 2 - JavaNarayanView Answer on Stackoverflow
Solution 3 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 4 - JavamikuView Answer on Stackoverflow
Solution 5 - JavaChristian VermeulenView Answer on Stackoverflow
Solution 6 - JavaTim TView Answer on Stackoverflow
Solution 7 - JavaJimmyView Answer on Stackoverflow
Solution 8 - JavaMkneView Answer on Stackoverflow
Solution 9 - JavaSuraj ChandranView Answer on Stackoverflow
Solution 10 - JavaamekusaView Answer on Stackoverflow