Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java naming convention for boolean variable names: writerEnabled vs writerIsEnabled

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)
 {
    //...
 } 
like image 218
CodeBlue Avatar asked Aug 13 '12 20:08

CodeBlue


People also ask

How do you name a boolean variable in Java?

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.

What are the 3 rules for naming variables in Java?

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.

What is the naming convention for variables in Java?

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.


2 Answers

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.

like image 168
Fritz Avatar answered Sep 30 '22 17:09

Fritz


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.

like image 36
xthexder Avatar answered Sep 30 '22 17:09

xthexder