简体   繁体   English

Java boolean getter "is" vs "are"

[英]Java boolean getters "is" vs "are"

I know that the convention in Java for boolean getters is include the prefix "is".我知道 Java 中布尔 getter 的约定是包含前缀“is”。

isEnabled
isStoreOpen

But what if the subject is plural?但是如果主语是复数呢? That is, what if instead of wanting to know if a store is open, I wanted to know if all the stores are open?也就是说,如果我不想知道一家商店是否营业,而是想知道所有商店是否都营业,该怎么办?

isStoresOpen() doesn't make sense in English. isStoresOpen()在英语中没有意义。

I'm tempted to write getters like:我很想写像这样的吸气剂:

areStoresOpen
areDogsCute
areCatsFuzzy

And I think that would make sense, but I've been told by others that I should just suck it up and abandon subject verb agreement and use isStoresOpen , isDogsCute , isCatsFuzzy .我认为这是有道理的,但其他人告诉我,我应该接受它并放弃主语动词协议并使用isStoresOpenisDogsCuteisCatsFuzzy

Anyway, what should I do for boolean getters which operate on a plural subject?无论如何,对于在复数主题上操作的布尔吸气剂,我应该怎么做?

How about having decent enough english and following Java standard:拥有足够体面的英语并遵循 Java 标准怎么样:

  • **are**StoresOpen() > to be >> isEveryStoreOpen() **are**StoresOpen() > 是 >> isEveryStoreOpen()
  • **are**CatsCute() > to be >> isEachCatCute() **are**CatsCute() > 是 >> isEachCatCute()

When in doubt of the right word I always like to hit up the thesaurus.当对正确的词有疑问时,我总是喜欢查询词库。

I can't remember which book this was from, but the essence is that code will be read many more times than it's written.我不记得这是出自哪本书,但本质是代码的阅读次数比编写次数要多得多。 Write for readability.为了可读性而写。

The convention is to prefix the getter-method with "is" not the variale itself.约定是在 getter 方法前面加上“is”而不是变量本身。

eg例如

private boolean enabled;

public boolean isEnabled() {
    return enabled;
}

and

private boolean storesOpen;

public boolean isStoresOpen() {
    return storesOpen;
}

isStoresOpen() doesn't make sense in English. isStoresOpen() 在英语中没有意义。

It might not make sense grammatically, but it follows the convention and looks readable enough.它在语法上可能没有意义,但它遵循约定并且看起来足够可读。

The Java Bean specification says to use get for getters unless it's a boolean then use is . Java Bean 规范说 getter 使用get ,除非它是boolean ,然后使用is are is non-standard and will not be recognized by anything that expects standard Bean naming. are是非标准的,不会被任何期望标准 Bean 命名的东西识别。

Lots of tools expect is or get and won't likely recognize are .许多工具期望isget并且不太可能识别are

Try rephrasing them, like getDogsAreFuzzy() or getStoresAreOpen() or things like that for better compatibility and conventions.尝试改写它们,例如getDogsAreFuzzy()getStoresAreOpen()或类似的东西,以获得更好的兼容性和约定。

What do you code, English or Java ?你用什么编码,英语还是Java

When I read Java code, I expect things to be structural.当我阅读 Java 代码时,我希望事情是结构化的。 Boolean methods starting with is is a good structure.is开头的布尔方法是一个很好的结构。

return 0; 

- isEnabled() can also be written as getEnabled() in Java naming conventions . -Java naming conventions中, isEnabled()也可以写成getEnabled()

- Its just a good habit to follow the naming conventions, help when you are working with Java Beans . -遵循命名约定只是一个好习惯,在您使用Java Beans时会有所帮助。

In general I think code should be as easily readable as possible so that a method can almost be read as a paragraph (as espoused by Clean Code ).总的来说,我认为代码应该尽可能容易阅读,这样一个方法几乎可以作为一个段落来阅读(正如Clean Code所支持的那样)。 Therefore, I would name the method to sound / read as easily as possible and go with the grammer rule of are .因此,我将尽可能轻松地命名为发音/阅读的方法,并遵循are的语法规则。 With modern IDEs it is easy to find methods without looking specifically for get / is .使用现代 IDE 很容易找到方法,而无需专门寻找get / is

However, Kumar makes a good point about beans.然而,库马尔对豆类提出了一个很好的观点。 A lot of tools will only look for get / is .很多工具只会寻找get / is In that case I might consider having both methods.在那种情况下,我可能会考虑两种方法。 One for ease of reading and one for tool use.一种便于阅读,一种用于工具使用。

In your question you're explicitly asking about getters.在您的问题中,您明确询问了吸气剂。 A getter returns some information about one instance of your class. getter 返回有关您的类的一个实例的一些信息。 For example you have a class Store .例如,您有一个类Store Now, isStoreOpen is a perfectly fine method name for a getter.现在, isStoreOpen是一个非常好的 getter 方法名称。

Next, you mention a method that checks if all stores are open.接下来,您提到了一种检查所有商店是否都营业的方法。 This method isn't a getter at all, because it doesn't return information about one instance but for all.这个方法根本不是一个 getter,因为它不返回关于一个实例的信息,而是返回所有实例的信息。 Of course unless there is a class Stores .当然除非有类Stores If this is the case, you should rethink your design, because Java already has ways to store a number of instances, eg arrays or collections, so you don't have to write extra classes.如果是这种情况,您应该重新考虑您的设计,因为 Java 已经有存储大量实例的方法,例如数组或集合,因此您不必编写额外的类。

If this is not the case, then this method name is perfectly fine.如果不是这种情况,那么这个方法名称就很好了。 An alternative may be just allStoresOpen without the 'is'.另一种选择可能只是allStoresOpen没有'is'。

TL;DR: If you're dealing with multiple instances, it's not a getter. TL;DR:如果您要处理多个实例,则它不是吸气剂。 If it is, your design is bad.如果是,你的设计很糟糕。

Quite honestly I would say definitely forget about the are* and stick with is* .老实说,我会说绝对忘记are*并坚持使用is* Think of the "is" as the variable meaning and make a better name if possible."is"视为变量含义,并尽可能取一个更好的名称。

I would say is isStoresOpen doesn't sound that bad, but you can make isStoresAreOpen if that sounds better for you.我想说 isStoresOpen 听起来还不错,但是如果听起来更适合您,您可以制作 isStoresAreOpen 。

But my general idea would be to stick to the conventions.但我的总体想法是遵守约定。 Which is using "get" for getters and "is" for boolean types.它对 getter 使用“get”,对布尔类型使用“is”。 Personally I think using "is" is sometimes already problematic.我个人认为使用“is”有时已经存在问题。 Yes - it does look good in "if" conditions, but sometimes I just write "get" when coding and check the drop down list for my needed variable and start wondering what's wrong and why I can't find it, then I realize it starts with "is"...是的 - 它在“if”条件下看起来确实不错,但有时我只是在编码时写“get”并检查下拉列表中我需要的变量并开始想知道什么是错的以及为什么我找不到它,然后我意识到它以“是”开头...

In object-oriented programming, this should rarely, if ever, occur since Store or Cat or what have you should be a separate class, with its own isOpen() or isFuzzy() method.在面向对象的编程中,这应该很少发生,如果有的话,因为StoreCat或者你有什么应该是一个单独的类,有它自己的isOpen()isFuzzy()方法。 If you have a higher type, consider splitting down to the more atomic level that you're actually using.如果您有更高的类型,请考虑分解到您实际使用的更原子级别。 In general, objects should not be plural at the lowest level.一般来说,对象在最低级别不应该是复数。

isStoresOpen() in this StoresOpen is seems like a plural,这个 StoresOpen 中的isStoresOpen()似乎是复数,

When you follow that Java Naming Convention and Java Beans Standards, they have predefined prefix's for boolean and other type, so you should follow Java Beans Naming Convention.当您遵循 Java 命名约定和 Java Beans 标准时,它们为布尔值和其他类型预定义了前缀,因此您应该遵循 Java Beans 命名约定。

Let's come to your point When you see storesOpen as in an English prospective, yes it looks like plural.让我们来谈谈你的观点当你看到storesOpen在英语中的预期中,是的,它看起来像复数。 Once again take deep observation into that word,再次深入观察那个词,

Here这里

storesOpen is plural according to English grammar,根据英语语法, storesOpen是复数,

The out come of the isStoresOpen is not plural, instead of it is singular or you can say it is scalar in terms of programming convention. isStoresOpen的结果不是复数,而是单数,或者您可以说它是编程约定的标量。

It's out come is boolean, just true or false它的出来是布尔值,只是真或假

Not like your English plural statement true's or false's不像你的英语复数陈述true's or false's

Not an array of true or false , or not a collections of true or false不是truefalse的数组,也不是truefalse的集合

So, here we can say that, here we are concerned with value that is return from that boolean bean method, not the name given to the property of class to point real world entity.所以,在这里我们可以说,这里我们关心的是从该布尔 bean 方法返回的值,而不是赋予类属性以指向现实世界实体的名称。

One more important thing is, whenever such boolean properties are used in classes and those are used by predefined libraries in any framework, then framework with use prefix ' is ' for retrieving boolean values,更重要的一点是,每当在类中使用此类布尔属性并且任何框架中的预定义库使用这些属性时,使用前缀 ' is ' 的框架用于检索布尔值,

why means it's not that much of smarter than you as you know English grammar like plural/singular, multiplexer etc...为什么意味着它并不比你更聪明,因为你知道英语语法,如复数/单数,多路复用器等......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM