Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about boolean class properties naming convention for is/has [closed]

Tags:

java

naming

I have these properties for a patient represented by class Patient. I am wondering what is best practice to name them?

  1. whether she is in lactation period: boolean
  2. whether she is pregnant: boolean
  3. whether she is preparing for pregnant: boolean
  4. whether he/she suffers low blood glucose recently: boolean
  5. whether he/she has complications: boolean

a first thought was to name them as:

boolean isInLactationPeriod;
boolean isPregnant;
boolean isPreparingPregnant;
boolean hasSufferedLowBloodGlucoseRecently;
boolean hasComplications;

However I also come across suggestions that java properties should not be name with leading is / has, but to leave them to the getter / setter method, e.g.

boolean pregnant;
boolean isPregnant() {
    return pregnant;
}

Which one is better?

like image 236
xing Avatar asked Nov 19 '15 06:11

xing


People also ask

How should Boolean variables be named?

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.

Should boolean always start with is?

Not really, as booleans are not always used to indicate that an object "is" something. "has" is an equally valid prefix "was", "can" are also valid in particular circumstances, also, I have seen the suffix "Able" used. It all depends on what makes the program readable.

How do you name boolean properties?

✔️ DO name Boolean properties with an affirmative phrase ( CanSeek instead of CantSeek ). Optionally, you can also prefix Boolean properties with "Is", "Can", or "Has", but only where it adds value.

Which syntax is followed by Java for naming conventions?

Java follows camel-case syntax for naming the class, interface, method, and variable. If the name is combined with two words, the second word will start with uppercase letter always such as actionPerformed(), firstName, ActionEvent, ActionListener, etc.


2 Answers

It might be more of a convenience based or opinion based. But you can use the isPregnant as it is more or less making it clear.

See the Java docs:

8.3.2 Boolean properties

In addition, for boolean properties, we allow a getter method to match the pattern:

public boolean is();

This “is” method may be provided instead of a “get” method, or it may be provided in addition to a “get” method. In either case, if the “is” method is present for a boolean property then we will use the “is” method to read the property value. An example boolean property might be:

public boolean isMarsupial(); 
public void setMarsupial(boolean m);
like image 76
Rahul Tripathi Avatar answered Oct 01 '22 18:10

Rahul Tripathi


Both options looks good , but i will prefer the latter one.

is prefix can 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;

Please refer this

Also, naming field as pregnant looks more suitable and getters / setters names will make more sense. Is is a verb , which can be avoided being used in variable names.

like image 29
Ankur Singhal Avatar answered Oct 01 '22 18:10

Ankur Singhal