Stop using boolean type in your code

Stop using boolean type in your code

Data representation lies at the heart of every programming language, and one of the fundamental data types that developers encounter in virtually all systems is the boolean data type. Booleans, with their two possible states of true and false, excel at handling binary decisions, making them widely used and seemingly straightforward. However, as our systems evolve and business requirements grow more sophisticated, the simplicity of booleans becomes a double-edged sword. The rigid binary nature of booleans can hinder our ability to accommodate new states in our data, leading to tangled code and challenging maintenance. In this blog, we explore the limitations of using multiple boolean variables for data representation and discover a smarter alternative - Enums. Let's delve into how Enums empower us to handle diverse data states effectively and future-proof our code with ease.

Understanding the problem

The boolean data type is one of the fundamental programming data types which exist in every programming language. It is used to represent a binary state with values of true and false. Being one of the easiest data types, it is used quite often in every system heavily and that's where the problem begins. The very nature of a boolean variable is that it can be used to represent only two states. This prohibits change and doesn't allow us to add new meanings to our data. Business requirements evolve over time and what might look like binary state data now can easily evolve to become data with three or more states.

Let us take an example. Imagine you have a User table in which you wish to store the status of User account. Let us say that the user account can either be in Verified state or Not Verified. Since there are only two states, it seems simple enough at first look to make a boolean variable as such

isVerified = true;

We have got a simple variable which tells us whether or not the user is verified. It's pretty self-explanatory and easy to work with.

Now imagine, that the product requirements change over time and now there is a third possible state for user accounts to be in, such as Blocked.

Now we can introduce another boolean to accommodate this change.

isBlocked = false;

So now we have two booleans which tell us the status of our account.

isBlocked = false;
isVerified = true;

Anytime we have to perform any action on the user and we wish to know its status first, we must read the value from two different keys and compute the status based on that. Imagine if further new status values come in. Suddenly we have 3 boolean variables. And then so on and on. Further, handling all possible cases for all the booleans could become a very tedious task very easily.

Introduction to Enums

Enums, short for "Enumerations," are a powerful and intuitive data type found in almost every programming language. At their core, enums allow us to define a set of named, constant values, each representing a distinct state or option within a predefined collection. Unlike boolean variables that can only represent two states (true and false), enums empower developers to create custom data types that can have multiple meaningful values.

By using enums, developers can enhance code readability, maintainability, and scalability. They provide a more expressive and self-documenting way to work with data, making code easier to understand and maintain. Additionally, enums help prevent common errors caused by using raw numerical values or strings to represent states, as they enforce a strict type-checking system.

Instead of creating boolean variables (even for binary states), try using Enums instead. Enums allow you to use predefined constant values to define your data.

status = "VERIFIED";
status = "UNVERIFIED";
status = "BLOCKED";

If in future, the business requirements evolve, you can easily add new constant values to your set of Enums to allow new possibilities without any issues or complexity to your code.

Code examples

Here are some code implementations of Enums in some popular languages.

const Direction = Object.freeze({
    Up: Symbol('Up'),
    Down: Symbol('Down'),
    Right: Symbol('Right'),
    Left: Symbol('Left'),
});

console.log(Direction.Up);
from enum import Enum

class Direction(Enum):
    UP = "up"
    DOWN = "down"
    LEFT = "left"
    RIGHT = "right"

print(Direction.UP)
enum Direction {
    UP,
    DOWN,
    LEFT,
    RIGHT,
}

class Main {
    public static void main(String []args) {
        System.out.println(Direction.UP);
    }
}

A tabular comparison

BooleansEnums
Fundamental data typeUser-defined data-type
Easy to make. 0 effortA little bit of effort to define the constant values
Difficult to scale. Cannot add more states.Easy to scale. Can add as many states as needed.

Conclusion

In conclusion, while boolean variables have served as a fundamental data type in programming languages for representing binary states, they come with limitations. As business requirements evolve, the need to represent data with multiple states arises, making the use of multiple booleans cumbersome and error-prone. Fortunately, enums offer a simple and elegant solution, allowing us to define a set of predefined constants to represent various states. With enums, our code becomes more scalable, maintainable, and adaptable to changing business needs. By embracing enums as a powerful tool in our programming arsenal, we can confidently navigate the complexities of evolving data representations with ease.