Questions about naming conventions for boolean class properties for is / has

I have these properties for a patient represented by the Patient class. I wonder what is the best way to call them?

  • is she in lactation: boolean
  • Is She Pregnant: Boolean
  • is she preparing for pregnancy: boolean
  • Recently, low blood glucose: boolean
  • Does he have complications: boolean

The first thought was to name them as:

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

However, I also came across suggestions that java properties should not be names with leading names, has / has, but to leave them to the getter / setter method, for example.

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

Which one is better?

+5
source share
3 answers

It can be more based on convenience or based on opinions. But you can use isPregnant as it is more or less clear.

See Java docs:

8.3.2 Boolean properties

In addition, for Boolean properties, we allow the use of the getter method template:

public boolean is ();

This "eat" method may be provided instead of "receive", or it may be provided in addition to the "receive" method. In any case, if there is, the method exists for a logical property, then we will use the is is method to read the value of the property. An example Boolean property might be:

 public boolean isMarsupial(); public void setMarsupial(boolean m); 
+5
source

Both options look good, but I prefer the latter.

Prefix

can be used for boolean variables and methods. Iset, isVisible, isFinished, isFound, isOpen This is the naming convention for Boolean methods and variables used by Sun for Java kernel packages.

Using a prefix solves the general problem of choosing bad boolean names such as status or flag. isStatus or isFlag simply does not fit, and the programmer is forced to choose more meaningful names.

Setter methods for boolean variables must be prefixed as in:

void setFound (boolean isFound);

There are several alternatives to the prefix that is best suited to some situations. They have, can and should have prefixes:

boolean hasLicense (); boolean canEvaluate (); boolean shouldAbort = false;

Please contact this

In addition, the pregnant naming field looks more appropriate, and getters / qualifier names will make more sense. Is is a verb that can be avoided by using variable names.

+4
source

Agreement

Basically, SCJP 6 talks about bean naming conventions:

  • your variable should not have a;
  • getters must begin with get;
  • for booleans, get can be replaced with.

This is consistent with Rahul's answer .

There was nothing, so ideally, I think you should use this if your variable name is an adjective and it turned out differently.

Conventional Limits

Then, these are conventions, and people like me will appreciate that you respect them (as well as some tools and frameworks such as Spring), but in some cases it may make more sense to the reader. If you want to violate the agreement, it is up to you and your best opinion.

+1
source

Source: https://habr.com/ru/post/1236292/


All Articles