Java boolean getters "is" vs "are"

I know that the Java convention for boolean getters includes the "is" prefix.

isEnabled isStoreOpen 

But what if the subject is multiple? That is, if instead of finding out if the store was open, I wanted to know if all the stores were open?

isStoresOpen() does not make sense in English.

I am tempted to write getters like:

 areStoresOpen areDogsCute areCatsFuzzy 

And I think that would make sense, but others have told me that I should just suck it and abandon the consent agreement with the verb and use isStoresOpen , isDogsCute , isCatsFuzzy .

Anyway, what should I do for boolean recipients that work with multiple entities?

+59
java naming-conventions boolean
Oct 18 '12 at 17:43
source share
12 answers

I don’t remember which book it was from, but the point is that the code will be read many more times than it is written. Write for readability.

+52
Oct 18 '12 at 17:47
source share

How about having decent enough English and the following Java standard:

isEveryStoreOpen() or isEachCatCute()

When in doubt about the right word, I always like to hit the thesaurus.

+44
Mar 14 '14 at 6:47
source share

The convention is that the getter-method prefix with "is" is not variale itself.

eg.

 private boolean enabled; public boolean isEnabled() { return enabled; } 

and

 private boolean storesOpen; public boolean isStoresOpen() { return storesOpen; } 



isStoresOpen () does not make sense in English.

It may not make sense grammatically, but it follows the convention and looks readable enough.

+28
Oct 18
source share

Many tools expect is or get and are unlikely to recognize are .

Try rephrasing them, such as getDogsAreFuzzy() or getStoresAreOpen() , or something like this for better compatibility and conventions.

+16
Oct 18 '12 at 17:47
source share

The Java Bean specification uses get for getters if boolean is not used, then use is . are are non-standard and will not be recognized by anything expecting a standard Bean designation.

+14
Oct 18 '12 at 18:07
source share

- isEnabled() can also be written as getEnabled() in Java naming conventions .

- . Its just a good habit to follow the naming rules, help when you work with Java Beans .

+3
Oct. 18
source share

In general, I think the code should be as easy to read as possible, so the method can almost be read as a paragraph (as described in Clean Code ). So I would call the way to sound / read as simple as possible and go with the are grammar rule. With modern IDEs, it's easy to find methods without really looking at get / is .

However, Kumar makes a good conclusion about beans. Many tools will only search for get / is . In this case, I could consider using both methods. One for easy reading and one for using the tool.

+3
Oct 18 '12 at 17:47
source share

What language do you write: English or Java ?

When I read Java code, I expect that everything will be there, forcing me to search for both getters, with prefixes and prefixes, will be more difficult than finding only one prefix.

However, on the other hand, when I read the newspaper in the morning, I am not looking for anything, so you can write the English language in a more traditional way.

return 0;

+2
Oct 25
source share

In object-oriented programming, this should rarely happen, if ever, with a Store or Cat or that you should be a separate class with your own isOpen() or isFuzzy() method. If you have a higher type, consider splitting into a more atomic level that you are actually using. In general, objects should not be plural at the lowest level.

+1
Oct 18 '12 at 17:48
source share

In your question, you are explicitly asking about getters. The getter returns some information about a single instance of your class. For example, you have a Store class. Now isStoreOpen is the perfect method name for the recipient.

Then you specify a method that checks if all stores are open. This method is not a recipient at all, because it does not return information about one instance, but for all. Of course, if the Stores class does not exist. If so, you should rethink your design, because Java already has ways to store multiple instances, for example. arrays or collections, so you do not need to write additional classes.

If it is not, then this method name is excellent. An alternative can only be allStoresOpen without "is".

TL; DR: If you are dealing with multiple instances, this is not a getter. If so, your design is bad.

+1
Oct. 18 '12 at 19:12
source share

Honestly, I would say that definitely forget about are* and stick to is* . Think of "is" as the value of a variable and make a better name, if possible.

I would say isstoresOpen doesn't sound so bad, but you can do isStoresAreOpen if that sounds better for you.

But my general idea would be to stick to conventions. Which uses "get" for getters and "is" for boolean types. Personally, I believe that using is is sometimes problematic. Yes - it looks good in the conditions of "if", but sometimes I just write "get" when coding and check the drop-down list for my necessary variable and start to wonder what is wrong and why I can not find it, then I understand it with "is" ...

0
Apr 11 '16 at 13:38 on
source share

isStoresOpen () in this StoresOpen looks like the plural,

When you follow these rules of the Java Naming Convention and Java Beans, they have a predefined prefix for Boolean and other types, so you must follow the Java Beans Naming Convention.

Come to you. When you see storeOpen , as in the English perspective, yes, it looks like a plural. Once again, take a deep look at this word,

Here

storeOpen - plural according to English grammar,

Exiting isStoresOpen is not plural, not singular, or you can say that it is scalar in terms of a programming convention.

This is logical, just true or false

Not like your English plural expression true or false

Not an array of true or false , or not a collection of true or false

So, here we can say that here we are dealing with a value that is a return from this logical bean method, and not with the name specified for the class property to indicate a real-world object.

Another important thing: whenever such logical properties are used in classes, and they are used by predefined libraries in any structure, then a framework with the prefix pre "to get the logical values ​​p>

why does this mean that he is not so much smarter than you, as you know the English grammar as plural / singular, multiplexer, etc.

0
Jun 09 '17 at 10:36 on
source share



All Articles