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)
{
//...
}
Boolean variables should be prefixed with 'is' 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.
Step 1 − All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_). Step 2 − After the first character, identifiers can have any combination of characters. Step 3 − A keyword cannot be used as an identifier.
For variables, the Java naming convention is to always start with a lowercase letter and then capitalize the first letter of every subsequent word. Variables in Java are not allowed to contain white space, so variables made from compound words are to be written with a lower camel case syntax.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With