Could we stop prefixing booleans with the verb ?
Martin Goyot
Posted on February 7, 2024
A widespread convention when it comes to Boolean variables naming is to prefix them with a verb. Sometimes the explanation given for this practice is that it helps reading code, other times that this way we directly can spot that this is indeed a boolean. I would like to discuss those two assumptions.
It helps reading code
Maybe this is just me but I like when code reads like instructions. I agree that it can't always be the case but I feel I should strive for it whenever I can. Keeping that in mind we can look at this complex piece of code:
if isUserLoggedIn {
}
Here we use the conventional prefixing, and the voice in my head reads: "If is user logged in...".
Now, if we consider this piece of code without prefixing I read "If user is logged in...":
if userIsLoggedIn {
}
See, nothing spectacular, this won't change your life but still I feel like the second version is much more flowing than the first one, while prefixing "hurts" the readability. Please do note that we keep the is
or has
verbs, but at their natural placement, not as prefix.
It helps spotting that it is a Boolean
Let me be honest: this is totally true. Whenever I encounter a variable prefixed with this is
or has
, I definitely know that this is a Boolean.
What I would like to challenge here, is the need behind this, the use case. Do I really need to know that this is a boolean at this specific point in time. This might be terribly personal, but I distinguish at least two ways of reading code. The first one, which refers to the previous part of this short post, when I want to understand the flow of the code, and the second one, when I'm diving into the code to edit it or spot an bug.
When I'm reading the code in a "I want to understand the flow" way, I don't really need to know that this is a boolean, I need a well named variable that helps me read what is happening, the fact that this is a boolean is a mere implementation detail that I don't need to know about. Moreover, I feel that reading userIsLoggedIn
leave very few room to interpret it as something else than a boolean. On the contrary, if I'm diving into the code to edit it or spot a bug, in this case, yes, I need to know. But, if I'm in this mindset, I'm much more into "details mode" and I will definitely check the type of my variable which will be declared in there and this will be the only source of truth for me, not the fact that it starts with is
or has
.
Conclusion
I guess that all this gibberish is about is that I try to prioritize readability when it comes to writing code, and I feel like this convention can hurt it. Now, if you are in a team where prefixing is a widespread convention, please do follow this rule or challenge it with the team but don't go rogue; being a team player is the one of the most important thing.
Posted on February 7, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.