Writing Maintainable Code — Boolean Condition Best Practices
September 13, 2025
In our "Path to Senior Engineer" series, we explore insights from industry veterans to help developers write more maintainable code.
This article summarizes key insights from former Google senior engineer Max Kanat-Alexander's article "Improve Readability With Positive Booleans."
What makes code feel natural to read? Max suggests that healthy code should flow like reading in your native language. When you find yourself pausing to decipher what a line of code does, that's a signal that the code's readability needs improvement.
He shares a simple but powerful technique for boolean conditions: structure your boolean checks to test for positive conditions rather than negative ones. Consider this extreme example:
if not nodisable_kryponite_shield:
devise_clever_escape_plan()
else:
engage_in_epic_battle()
What's happening in this code? Most people can figure it out eventually, but it requires mental effort. How might we make this easier to understand?
The solution involves transforming negative checks into positive ones. Instead of testing for the absence, disabling, or falseness of something, check for the presence, enabling, or truth of a condition.
Here's a clearer version:
if not enable_kryponite_shield:
devise_clever_escape_plan()
else:
engage_in_epic_battle()
Notice how changing the variable name from nodisable_kryponite_shield
to enable_kryponite_shield
makes the logic easier to follow. But we can make this even clearer.
Restructuring for Maximum Clarity
When you see patterns like if not ... else ...
, consider flipping the condition to put the positive case first:
if enable_kryponite_shield:
engage_in_epic_battle()
else:
devise_clever_escape_plan()
This transformation makes the code's intent crystal clear. We're checking if the shield is enabled, and if so, we engage in battle. Otherwise, we escape.
Why Positive Conditions Work Better
Our brains process positive statements more efficiently than negative ones. This principle extends beyond code into many areas of communication and design.
Consider command-line flags: foo --disable_feature=False
requires more mental processing than foo --enable_feature=True
. The double negative in the first example forces us to work through the logic step by step, while the positive version communicates intent immediately.
When you change default settings to enable a feature, the cognitive burden becomes even more apparent. Users must mentally navigate through multiple layers of negation to understand the final state.
Recognizing Exceptions
Are there cases where negative conditions make sense? Absolutely. In Python, if foo is not None
can be considered a positive check even though it contains not
. We're positively verifying that a value exists.
The key insight is context. Sometimes checking for the absence of something is the most natural way to express a condition. But in general, testing for the presence or truth of conditions creates more readable code than testing for absence or falseness.
Building Intuitive Boolean Logic
This technique works across programming languages and contexts. When designing APIs, variable names, or user interfaces, consider how positive framing can reduce cognitive load.
The next time you write a boolean condition, ask yourself: "Am I checking for what I want to be true, or what I want to be false?" Often, restructuring around the positive case will make your code more intuitive and maintainable.
Support ExplainThis
If you found this content helpful, please consider supporting our work with a one-time donation of whatever amount feels right to you through this Buy Me a Coffee page, or share the article with your friends to help us reach more readers.
Creating in-depth technical content takes significant time. Your support helps us continue producing high-quality educational content accessible to everyone.