I have these properties for a patient represented by class Patient. I am wondering what is best practice to name them?
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?
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.
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.
✔️ 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.
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.
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);
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.
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