Originally posted at michaelzanggl.com. Subscribe to my newsletter to never miss out on new content.
There is a convention to prefix boolean variables and function names with "is" or "has". You know, something like isLoggedIn, hasAccess or things like that.
But throughout my career I have seen and written code where this convention was just thrown out the window. So let's check out some of these edge cases.
As with all rules, there are exceptions and it might be better to go with that rather than enforcing a convention.
Boolean that verifies that every case is true
const XXX = users.every(user => user.isActive)
XXX will only be true if every user is active. How could we name the variable?
| variable | Any good? | Reason |
|---|---|---|
| isUsersLoggedIn | ๐คจ | Grammatically incorrect |
| areUsersLoggedIn | ๐ค | Custom prefix |
| isEveryUserLoggedIn | ๐ | Fits with Array.prototype.every
|
| isEachUserLoggedIn | ๐ฅฐ | Comes off more natural than "every" (depends) |
Boolean that verifies that one of many cases is true
const XXX = users.some(user => user.isActive)
XXX will only be true if at least one user is active.
| variable | Any Good? | Reason |
|---|---|---|
| isUsersActive | ๐ | Grammatically incorrect & ambiguous |
| isAtLeastOneUserActive | ๐ต | Too wordy |
| isOneUserActive | ๐คฅ | Lie. This could imply that there is only one user active. Avoid confusion!!! |
| isSomeUserActive | ๐ | Fits with Array.prototype.some
|
| isAnyUserActive | ๐ค | Comes off more natural than "some" (depends) |
Avoid custom prefixes
We covered this in one of the examples before already, but there are more...
| variable | Any good? | Reason |
|---|---|---|
| wasPaidFor | ๐ค | Custom prefix |
| areBillsPaidFor | ๐ค | Custom prefix |
| hadHaveHadBeenPaidFor | ๐ถ | Ok, I'm just joking at this point |
| isPaidFor | ๐ |
Affirmative names
| variable | Any good? | Reason |
|---|---|---|
| isDisabled | ๐ง | Negative |
| isNotActive | ๐คฏ | Just imagine !isNotActive
|
| hasNoBillingAddress | ๐ | No need for "no" |
| isEnabled / isActive / hasBillingAddress | ๐ | And use it like this !isActive to get the negative |
The problem with negative variable names becomes most apparent when you have something like this
if (!account.isDisabled) {
// ...
}
Just see how much easier this reads
if (account.isEnabled) {
// ...
}
Finally let's take a look at a more complex example.
const isAnyUserOffline = users.some(user => !user.isOnline)
if (isAnyUserOffline) {
// ...
}
While this works, because of the combination of some and !, it just takes a little more brain power to process this code. An alternative would be:
const isEveryUserOnline = users.every(user => user.isOnline)
if (!isEveryUserOnline) {
// ...
}
It behaves the same and as long as the data set is small enough I would not worry about the small performance impact.
I am sure there is a lot I missed, but I think these are some of the more common cases.
Latest comments (57)
Great !
hadHaveHadBeenPaidForthis made me chuckle on the middle of the day, thank you :))
Great article, very useful, thanks
what about shouldSave as a boolean prop to a component that would fire the useEffect?
answering what should save would be easier to understand.
recordShouldSaveHow about naming something which really cannot be smaller? Like an option for
Generate file when user logs in everytime
โwhen user logs in everytimeโ isnโt the part of assertion but rather a descriptive requirement. The assertion here is something like
fileShouldBeGenerated.Great article, thank you!
Found it, trying to find idea for naming of boolean "AreContactsShown". Name "isEachContactShown" would be weird - there are no separate contact items.
Any suggestions?
contactsAreShown,contactsAreVisible, or create an object:contacts.visibleThanks!
Where are the contacts displayed in? You could maybe use that instead, e.g. isContactListShown
This whole naming of booleans is one of the simplest yet most misunderstood conventions in programming.
You seem to be a proponent of naming booleans as affirmative statements, yet you name your booleans as questions. This seems contradictory to me.
isEveryUserOnline? Is he? I don't know, maybe? Are you asking me? Or is anyone standing behind me? Why is code talking to me and making me decide? Code is a set of commands. It answers questions. It doesn't ask them.
Purely linguistically, you don't say "If am I a developer". You say "If I am a developer".
Your
isEveryUserOnlinevariable should be namedeveryUserIsOnlineorallUsersAreOnline. This would truly be affirmative, according to your own standard. This will also read well in English and go well with order of words when using dot notation. Compare:The only standard you really need for booleans is an affirmative statement that evaluates to either true or false. That's it. Hello, Propositional Calculus. Is, are, has, was, did, will, should, must, can: all just flavors.
As for prefixing for the sake of prefixing, I thought that was called Hungarian notation and is more or less dead, with a few relics still roaming the Earth.
Hope this helps,
Cheers
Thank you, exactly how Iโve been feeling. Affirmative names are also great at negating:
!user.online,!sessionActive,!changesSaved.Don't forget the verb "to be": user.isOnline, sessionIsActive, changesAre/WereSaved.
I would say that the variable name is indeed a question and the variable value is the answer to said question.
In english, a sentence beginning with a verb is a question most of the time. So for foreigners, it's a no brainer a variable starting with a questioning verb is a boolean.
I get it that native english speakers don't really talk the way the grammar is technically but this is how non native people learn the language, by learning and studying grammar first. And reading is different than speaking. We're reading code every day, not speaking. To simply ignore it because of use is a cultural thing and not really in the rules of the language.
Just my thoughts and point of view, from other perspective, to add to the conversation. But in the end of the day, it's good to be consistent. That's all.
This is factually incorrect. In English, sentences starting with a verb are imperative most of the time.
I've been wasting more time than I should agonizing over this, but I agree with Stephan. I prefer everyUserIsOnline over isEveryUserOnline. That way I can write: "if (everyUserIsOnline)" and it reads like an English sentence. Couldn't be more clear what's happening.
I was tempted to go along with the original post, but when trying to actually use something like, "if (isEveryUserOnline)"... Just... No. Just feels wrong. Is every user online? I dunno. "If is every" wtf does that even mean? And you rule out other options for being ungrammatical?
Yes, I'm a programmer and know how booleans work, I could understand if it was named oaentuh0296eoau, especially with the help of VSCode Intellisense, so then why bother with any of this? Seems to me the more it reads like an actual sentence, the more clear it is what's going on.
And why insist on having is as a prefix? For the sake of adhering to some pseudo-Hungarian notation style that we use nowhere else? At the expense of clarity? I don't see the point.
Seems very pedantic. The word
isisn't asking you, it's invoking a question with a binary answer.userIsActiveis a statement. It makes it sound like a constant.Take for example "Is it 5 o'clock" would invoke a yes/no answer in your mind immediately. If your brain suddenly thinks that the computer is talking to you, rather than you realizing that the variable is actually saving a boolean, it's a problem between chair and keyboard.
Conversely, with your format, "It is 5 o'clock" would be a statement. You don't answer a statement, which makes it seem like a constant.
In fact, I would argue that statements are invocation of action. Because you wouldn't have a statement of "It is 5 o'clock" and then do nothing with it. So the name
userIsActivewould be a function i.e.userIsActive(), which you would use as inFor invocation of actions in the imperative programming paradigm, with which you (arguably) unbeknownst to yourself operate, imperative mood of verbs is conventionally used. This is what gave imperative programming paradigm its very name.
PS: In an educated and intelligent community such as ours we do not call people "a problem between the chair and the keyboard" just because we disagree. Disagree one may. Insult one may not. Assuming education and intelligence are there in the first place, of course.
I created an account here just to <3 this comment.
It's not important at all that the auxiliary verb (is, has, was, etc) is a prefix, or not. It's not important that it be singular, and not plural. It's not important what temporal tense it's in.
It's about expressing a boolean value so that any English reading person could understand it, even if s/he doesn't know any programming principles. Make it literal and readable.
everyUserIsActive
someUsersAreActive
wasSuccessfullyUpdated
user.isAdmin
The variable should not beg the question, "what does this even mean?" or "what is this referring to?" It should be perfectly clear by simply reading it.
My other boolean pet peeve is this:
if($user->isAdmin) {
return true;
}
else {
return false;
}
Why not:
return $user->isAdmin;
?
absoulutely agree. i understand the need for the convention, since ( expecially at the beginning of your career) you are able to immediatly find or recogninze a boolean variable by the fact that starts with is or has, but was never bought into this.
exactly for the reason you mentioned,
and because of the many exceptions that make some variables seriously grammatically terrible. I found this post because I was looking for alternatives or way of telling in a Pullrequest that isUserAlreadyExist is a complete NOPE for me...
imho, dimply use conventions when it makes sense, ( possiblly in thex affermative format - like useIsActive - which in my case would become a more natural userAlreadyExists)
isExistingUser
What about the point that no code ask you but you have asked the data about something and store answer in variable with the same name?
Then I already know the answer and can store it in a variable like
userExistsorsaved. Don't need to ask once again. If I want to ask, I would useIsSaved()orIsExistingUser().I especially love the pattern with some auto-generated codebase for based on OpenAPI or protobuf specs - the accessors
Getprefix irritates me even more.getIsActive()function always sounds to me as if by invoking this function I will get another function as a result, which will tell me if a user is active if I invoke it.I mostly agree with you that code readability should be one of the #1 priorities, but at the tend of the day it depends on the context -- also the whole "when in rome" thing.
I'll give you just another perspective: in my situation, I don't use Typescript at my every-day-work, so things are weakly typed, and the IDE auto-prediction works relatively well with VSCode.
Because of that, using is/has/should gives several benefits (magnified in a weakly typed language):
-> auto-prediction is actually very helpful when there's no ESDoc annotation written in common code.
-> consistency, and knowing immediately what something is (though I'd agree and enjoy to work on more other-people's code which is thought out to read-well and flow well as you seem to encourage). There's an aspect of things where if you learn to speed read, the effect when you're glancing over words implies that you catch the first-and-last-part-of-words.
-> when interfacing APIs/other things, getters/setters are easy to automate code with (at least for JS/Ruby), and it's easy to write linter rules on weakly typed languages.
Great article, thanks !
Question, how do you name boolean getters ?
I've seen people using booleans like "empty" and getters like "isEmpty()", but I prefer having is/has prefixes for booleans as you say.
I use getters like "getIsEmpty()" for an "isEmpty" boolean, but many people find it akward..
I prefer to leave verbs for functions, such as getters.
Many people do but stand your ground, it's good.
Sometimes, one can be prefixed with has or did.
isTwoLoggedIn?
areTwoLoggedIn?
isThereTwoLoggedIn?
What should it be? Any other option?
Two of whom? :)
twoUsersAreLoggedIn
You changed my mind with
Following that logic, you don't really need is as prefix, and naming of boolean follows the logic nicelly, much more readable, becouse I tryed changing all is to 'command', suprisingly it's just much more readable and parsable in by my head.
isSendEmailFetching -> (request is made to job to send email)
sendingEmail
I wonder what do you think about using transitive verb like this?
Thank you
Alternatively, in passive voice, emailIsBeingSent.
Good stuff
That's a tough one.
What came to my mind was also
isPairLoggedIn
isPairOfUsersLoggedIn
Never ran into this case before though :D
Horrid