Code Smell 07 - Boolean Variables

Code Smell 07 - Boolean Variables

Maxi Contieri
·Oct 26, 2020·

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Using boolean variables as flags, exposes accidental implementation and pollutes the code with Ifs.

TL;DR: Don't use boolean variables, they force you to write Ifs. Create polymorphic states instead.


  • Extensibility

  • Comparison in some languages


  • If Boolean maps to a real world entity is safe. Otherwise, model as a State to favor Extensibility. This also follows Open/Closed Principle.


  • Flags


  • Real world true/false rules

Sample Code



function processBatch(
    bool $useLogin,
    bool $deleteEntries,
    bool $beforeToday) {

function processBatch(
    LoginStrategy $login,
    DeletionPolicy $deletionPolicy,
    Date $cutoffDate) {


Automatic detection can warn for boolean usage, but this can yield false positives.


Some languages have issues with boolean comparators.


In these coupled with accidental complexity languages, booleans are a common error source.

Also Known as

  • Flag Abuser


  • Declarative

  • Primitive

More info


Take extra care when declaring something boolean. Flags are difficult to maintain and extend. Learn more about the domain. Try migrating to state design pattern. Use polymorphism instead of ifs/switch/cases.


Photo by Phil Hearing on Unsplash

These tweets inspired this code smell:

This article is part of the CodeSmell Series.

Last update: 2021/06/08

Share this


Technical Opinions are my own. I don't have the revealed truth.

Software Design is a creative activity. These are hints and not rigid rules.

I write on BackEnd Business Systems and OOP Design. My advice/experience might not suit other systems.

You can write me at info(at)