DEV Community

Cover image for Effective Naming of Booleans

Effective Naming of Booleans

matthewcmckenna profile image Matt McKenna Originally published at ・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.

var hasNoBananas = true
Enter fullscreen mode Exit fullscreen mode

and "No! We have no bananas" into the false equivalent

var hasNoBananas = false
Enter fullscreen mode Exit fullscreen mode

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


if (hasNoBananas) {
  println("We have no bananas")
} else {
  println("We have bananas")
Enter fullscreen mode Exit fullscreen mode

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.

var isNotEnabled = true
Enter fullscreen mode Exit fullscreen mode

This looks just like the above, 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:

Enter fullscreen mode Exit fullscreen mode

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. 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:

var isNotNOTEnabled = true
Enter fullscreen mode Exit fullscreen mode

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

var hasBananas = true

fun answerFruit(fruit: String, hasInventory: Boolean) {
    if (hasInventory) {
      println("We have $fruit")
    } else {
      println("We have no $fruits")

answerFruit("bananas", hasBananas) // We have bananas

hasBananas = false

answerFruit("bananas", hasBananas) // We have no bananas
Enter fullscreen mode Exit fullscreen mode
var isEnabled = false

isEnabled = true
Enter fullscreen mode Exit fullscreen mode

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


Editor guide