17

Which of the following declarations conforms to Java's naming conventions?

private boolean writerIsEnabled;
// with methods like
public boolean getWriterIsEnabled() 
public void setWriterIsEnabled()

OR

private boolean writerEnabled;
// with methods like
public boolean getWriterEnabled() 
public void setWriterEnabled()

I personally find the first name "writerIsEnabled" to be more readable, especially when you use it in an if statement like this -

if(writerIsEnabled)
 {
    //...
 } 
informatik01
  • 16,038
  • 10
  • 74
  • 104
CodeBlue
  • 14,631
  • 33
  • 94
  • 132
  • 1
    possible duplicate of [Valid java bean names for booleans](http://stackoverflow.com/questions/799280/valid-java-bean-names-for-booleans) – Andreas Dolk Aug 13 '12 at 20:08
  • 5
    Personally, I'd err on the side of `[is]WriterEnabled`. This means I would call the variable `writerEnabled` and the getter `isWriterEnabled` and the setter `setWriterEnabled`. This comes more from code auto-completion then convention though. – MadProgrammer Aug 13 '12 at 20:09

4 Answers4

33

As far as I know, it's this way:

private boolean writerEnabled;
// with methods like
public boolean isWriterEnabled();
public void setWriterEnabled(boolean enabled);

Either when the type is boolean or Boolean, the difference is that the Getter starts with is instead of get.

Personally I prefer the isWriterEnabled approach. Technologies like, for example, JSF respect that standard when accessing properties. The EL expressions are acknowledged with is and get.

Fritz
  • 9,987
  • 4
  • 30
  • 49
5

If this is in a writer class, you'd probably want to remove the Writer from your variable.

I would typically not use Is in my field names, but would in the methods.

Something like this:

private boolean writerEnabled;

public boolean isWriterEnabled();
public void setWriterEnabled(boolean enabled);

Although this is my personal naming convention, you should probably talk with any others who you're working with, to see what they would use.

xthexder
  • 1,555
  • 10
  • 22
3
private boolean writerEnabled;
public boolean isWriterEnabled() 
public void setWriterEnabled()
Aaron Kurtzhals
  • 2,036
  • 3
  • 17
  • 21
2

For the getter and setter methods, I believe the convention is public boolean isWriterEnabled() and public boolean isReaderEnabled(). As for the variable, it should be private boolean writerEnabled.

Qman
  • 377
  • 1
  • 4
  • 14