Navigate back to the homepage

Effective Naming of Booleans

Matt McKenna
November 21st, 2020 · 2 min read

“Do you have bananas?”

“Yes! We have no bananas.”

This is the title of a song by Frank Silver and Irvine Cohn that my great grandfather used to play for me on his ukulele, but today we will be using it to better understand how to name boolean values.

This line is comedic in its confusion. Upon asking a yes or no question the inquisitor is met with both in response. How are they supposed to interpret this answer? After a moment of thought it can be fairly safe to determine the shopkeeper has no bananas. That extra moment of thought leading to this conclusion is what we will be discussing. 

Lets translate this question into some Kotlin code. We can turn “Yes! We have no bananas into the below boolean declaration.

1var hasNoBananas = true

and “No! We have no bananas” into the false equivalent

1var hasNoBananas = false

This code is perfectly valid and won’t cause any problems in its implementation. 

1if (hasNoBananas) {
2 println("We have no bananas")
3} else {
4 println("We have bananas")

Let’s take a step away from the bananas for a moment and look at a more practical example. Let’s consider a feature flag that will determine whether or not our users have access to a new part of our app and name it with the same intention we have been looking at. We can do this in two ways depending on the words we are using. With the inclusion of ‘not’ or using a negated version of the word itself.

1var isNotEnabled = true
2var isDisabled = true

This looks just like the example with the bananas, but when applied to our context, it has the same effect as the reply from the shopkeeper. The ‘not’ in the boolean name creates a moment of mental processing needed to understand that true is actually the negative case and false is actually the positive case. Much like a double negative in grammar, the reader of this code needs to apply more thought to what will happen when presented with:

1fun setFeatureEnabled(isEnabled: Boolean) {
2 if (isEnabled) {
3 // Enable Feature
4 } else {
5 // Disable Feature
6 }
10setFeatureEnabled(isNotEnabled) // setFeatureEnabled(true)
11// or
12setFeatureEnabled(isDisabled) // setFeatureEnabled(true)

We have added a negation into the name of our boolean. We are asking the reader of our code to care about the name as well as the current value to understand what we are intending. In the above example we are passing true to our function with the names isNotEnabled and isDisabled. This will lead to confusion as the function is expecting true to be the enabled case, but reading this code it looks like we want to disable our feature.

When naming a boolean it is best to omit any modifiers to the intention of the value in the name. We wouldn’t write something like:

1var isNotNOTEnabled = true

so we should also not write something like:

1var isNotEnabled = true

This also applies to words that can have negated meaning as we could easily name a boolean like:

1var isDisabled = true

But this has the same problem of needing to align the positive true with the negated naming and vice-versa. And just like above we wouldn’t write:

1var isNotDisabled = true

This would be clearer and logically equivalent if it was named isEnabled. Therefore, we should rely on the positive naming scheme to keep the true / false values aligned with the boolean that they represent.

Applying this understanding to our case

Making the changes to the above we can see how effective this is, both for the shopkeeper and our app!

1var hasBananas = true
3fun answerFruit(fruit: String, hasInventory: Boolean) {
4 if (hasInventory) {
5 println("We have $fruit")
6 } else {
7 println("We have no $fruits")
8 }
11answerFruit("bananas", hasBananas) // We have bananas
13hasBananas = false
15answerFruit("bananas", hasBananas) // We have no bananas
1var isEnabled = false
2setFeatureEnabled(isEnabled) // setFeatureEnabled(false)
4isEnabled = true
5setFeatureEnabled(isEnabled) // setFeatureEnabled(true)

By removing the negation in the name, we now understand and define the desired intent. Applying this practice prevents needing to make any mental jumps when working with the humble boolean.

Leave a comment on Twitter with your thoughts!

Hey! We've got a newsletter!

Your weekly dose of bite sized Android news and events delivered Monday mornings to kick off your week.

Each issue contains TL;DRs of recent articles, a developer tool highlight, interview practice, and a stoic sentiment applied to tech careers.

All with a No-Spam Guarantee.

Read more about it here!

More articles from StoicallyTyped

Magically turn your phone into a tablet with adb for testing tablet views!

Learn how to use adb to change a phone's screen size and density to render tablet views.

March 28th, 2020 · 3 min read

Hello, World!

This is the start of my blogging journey!

February 20th, 2020 · 1 min read
© 2020–2021 StoicallyTyped
Link to $ to $ to $ to $ to $