-2

There's a lot of free or member predicate-like functions (that returns boolean value) in different programming languages and popular libraries/frameworks that have "is" as a prefix, e.g.:

Sometime such function names don't have "is" at all, for instance empty for C++ std::basic_string.

But I can't recall or found in any big enough (not a homegrown) languages or frameworks/libraries name like the following:

  • ValueIsEmpty
  • word_is_short
  • pointIsValid

I.e. anything that has a form of affirmation rather than a question, so the "is" is located somewhere in the middle.

Could we say that affirmation form is not as good for predicate functions as question form or what is the reason why I didn't find that? Is there any popular software that use the affirmation form for mentioned scenario. Could you please provide some examples if they exist.

In other words do you know any library/framework where they prefer using:

  • ValueIsEmpty instead IsValueEmpty;
  • word_is_short instead is_word_short;
  • pointIsValid instead isPointValid etc.
4
  • @DownVoters if you know where NounIsAdjective form used, please just give me a link. Thanks in advance. Commented Nov 28, 2023 at 17:48
  • 1
    Asking for resources is off-topic. Commented Nov 28, 2023 at 18:24
  • @JörgWMittag is there suitable site across SE for such type of questions? Commented Nov 28, 2023 at 19:11
  • 1
    There is no one-size-fits-all answer to this question... also bear in mind that some words/prefixes may be reserved - eg in C any identifier beginning with is is reserved! So strictly speaking, isReserved() is a reserved identifier, as is island or issue Commented Dec 27, 2023 at 11:19

3 Answers 3

8

This inconsistency is a combination of things:

  1. Language designers are not required to adopt naming conventions from other languages when inventing a new programming language.

  2. Naming conventions have developed and evolved over the years by many different people.

  3. English grammar and meaning of the words being used in identifiers.

Points 1 and 2 are summed up as "because that's what people wanted to do." Point #3 is worth a little elaboration.

The context in which a word or phrased gets used helps determine its meaning. Consider the identifier empty used in the following lines of code:

  1. if (box.empty) — the context of the code using empty tells you it is a boolean value since it is used in an if statement. The language construct of the if statement provides the context to understand that empty is a question rather than a command. The word "empty" is being used as an adjective describing the noun "box".

  2. This next example uses the word "empty" as a verb, which changes the context of the conversation in the code:

    if (box.itemsCount > 0) { box.empty(); } ^^^^^ 

    Here, the word empty implies the box (which contains more than zero items) should have its contents removed. This appears to mutate the box object and would typically have the signature void empty() in many languages. The word "empty" implies an action — a verb.

Both uses are grammatically correct enough to be understood. It becomes personal preference whether you use empty or isEmpty in this case.

Other cases like ValueIsEmpty can be written as:

  • IsValueEmpty
  • EmptyValue (based on context again, like if (foo.EmptyValue)).

Same thing for word_is_short. It can be written as is_short_word and it means the same thing. Pluralizing words gets a little trickier in English because "is" becomes "are" in many cases:

// The word 'boxes' is plural, implying more than one box, // so we use 'are' instead of 'is': if (boxes.areEmpty) 

As a native English speaker, if (boxes.empty) feels awkward as a boolean, which implies a question, but for some reason if (box.empty) doesn't.

Other words like 'visible' are inherently adjectives which describe a noun. Furthermore, something is either visible or not, unless specified by a qualifier like "partially" (something is "partially visible"). Both visible and isVisible work in all the contexts in which I've seen them in programming. The addition of is does not provide additional context to the word visible. The word visible by itself is easy enough to understand.

In summary, there is inconsistency because humans are inconsistent. We never sat down and universally agreed upon these naming conventions. Things are further complicated by the meanings of identifiers and the contexts in which they are used. For these reasons you'll never find consensus on this.

Refer to a specific language for naming conventions. Failing that, refer to your team. Failing that, just make sure your code is understandable by having another person read it. Peer reviews of code are a great way to accomplish this.

8
  • 1
    "Refer to a specific language for naming conventions." – Indeed. In Ruby, for example, is_empty would not be idiomatic. Predicates are named with a question mark instead: empty?. The same applies to Scheme. Other languages mark predicates with a _p suffix. Yet others do it with -p. If you want to subscribe to the idea that types should tell you everything you need to know, the fact that the return type is bool should be enough and you don't need a naming convention as all. Commented Nov 28, 2023 at 13:34
  • Thank you for your answer. It seems that I weakly wrote my question, added a main issue what I'm interested to know. Commented Nov 28, 2023 at 14:08
  • @αλεχολυτ: to be honest, my answer remains unchanged with the edit to your question. There is no generally accepted preference here. Commented Nov 28, 2023 at 15:06
  • 2
    Now you're asking exactly for lists of external resources, which you've already been told is off-topic. It's also a really odd request, like "find me exactly 7 FOSS projects using 3-space indentation", or "does any CAD software have documentation written entirely in the passive voice"? Commented Nov 29, 2023 at 1:10
  • 2
    @αλεχολυτ, note that if(box.isEmpty()) reads like the "is" has moved to the middle. So in that sense, any OO project could serve as an example that prefers "is" in the middle. It is just mostly visible in the use location and not in the declaration/definition. Commented Nov 29, 2023 at 8:07
5

One main reason might be that the "noun" in your hypothetical examples is a variable or an expression on which the predicate is applied, so it would be redundant to specify it in the name of the predicate again.

For example, you're contrasting ValueIsEmpty (your preferred word order) to IsValueEmpty, which actually isn't used much either. What would be used would be value.IsEmpty() (message passing style) or IsEmpty(value) (function style).

In actual programs, you might find expressions like

words = input.SplitIntoWords(); if words.IsEmpty() ... 

when the writer wants to express that the result of splitting a line of text into words indeed yields words (or wants to do something else with them, which is more likely), or

if input.SplitIntoWords().IsEmpty() ... 

when it's not important to keep the list of words.

5
  • You mean that the both cases ValueIsEmpty and IsEmptyValue are uncommon, and much better to have extract distinct class to make object Value and have IsEmpty as a function for that type of object, i.e. use Value.IsEmpty() instead? In other words make additional class decompositions? Commented Nov 30, 2023 at 12:01
  • Not additional class decompositions. You already have the thing that you want to test for emptiness, right? Whatever it is named, that's its name. No need to add superfluous valueness to it. Commented Nov 30, 2023 at 12:15
  • For instance, I have some private object Value that have IsEmpty() function, but I would like to give transitive access thru outer object to mentioned function. What the most straight forward name you could suggest for it? From my POV the obvious name is IsValueEmpty. Commented Nov 30, 2023 at 12:46
  • In general, that's a bad idea because it's exposing the internal structure of your outer object. In many cases there might be a better name that's related to the outer object, not the inner objects. For example, a Parent object having children might expose a needsBabysitter() method, instead of giving other objects access to her children and letting them check whether they are under n years old. Commented Nov 30, 2023 at 13:11
  • But if that's absolutely not possible, an IsValueEmpty() method (as in "is your value empty?") may be unavoidable. Commented Nov 30, 2023 at 13:14
1

"is" or in some cases "has" should be the first part of the method name. For example

bool nothingThere = box.isEmpty() bool nobodyThere = !car.hasPassengers(); 

Without the is I would expect that empty() is a command to remove the contents of the box.

box.empty(); assert (box.isEmpty()); 

and if empty() is instead a boolean function then I could try to empty the box by calling box.empty() and nothing happens. I might get a warning at best.

Likewise I would expect that car.passengers() returns an array containing the passengers. is/has removes any doubt what you mean and that is a good thing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.