Groups | Search | Server Info | Login | Register


Groups > comp.programming > #16779

Re: “Booleans Considered Harmful”

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.programming
Subject Re: “Booleans Considered Harmful”
Date 2025-05-22 12:27 -0700
Organization None to speak of
Message-ID <87frgwxx7v.fsf@nosuchdomain.example.com> (permalink)
References <100mhh5$3b9hp$3@dont-email.me>

Show all headers | View raw


Lawrence D'Oliveiro <ldo@nz.invalid> writes:
> I think most of this article is a load of nonsense, myself.
>
> <https://www.infoworld.com/article/3990923/booleans-considered-harmful.html>
>
> Thoughts?

It's not *all* nonsense.

> 1. Stay positive
> 2. Put positive first

Ok, sure.  UserIsAuthorized is clearer than !UserIsNotAuthorized.

> 3. No complex expressions

Meh.  The example he gives is:

    if (user.age > 18 && user.isActive && !user.isBanned && user.subscriptionLevel >= 2) {
      grantAccess();
    }

I don't find that particularly problematic.  His suggestion to replace
each subexpression with a named constant isn't horrible, and it can be
useful in some cases, but it can also add clutter.

Note that the first three points are advice about how to use Booleans
clearly, not about avoiding them.  I guess a "Foo considered harmful"
title was too tempting.

> 4. Say no to Boolean parameters

This is perhaps his most valid point.  I agree that

    saveUser(user, true, false);

is too obscure.  But for languages that support them, named parameter
associations neatly solve the problem:

    saveUser(user, sendEmail = true, verified = false);

Unfortunately, a lot of very popular languages don't have this feature.

Comments are an alternative, but not a good one; it's too easy for them
to get out of sync with the code.

> 5. Booleans are a trap for future complexity

*Anything* could be a trap for future complexity.  The example is
a Boolean "isSmallDrink", which needs to be changed when the boss
announces we're going to be serving small, medium, and large drinks.

I agree that a 2-element emum (small, large) would have been clearer
and more extensible.  But refactoring is a thing, and revisiting
an earlier decision (changing the type of a variable) is something
that has to be done all the time.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

“Booleans Considered Harmful” Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-05-22 06:51 +0000
  Re: “Booleans Considered Harmful” Julio Di Egidio <julio@diegidio.name> - 2025-05-22 11:14 +0200
    Re: “Booleans Considered Harmful” Julio Di Egidio <julio@diegidio.name> - 2025-05-22 12:04 +0200
    Re: “Booleans Considered Harmful” Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-05-22 22:33 +0000
      Re: “Booleans Considered Harmful” Julio Di Egidio <julio@diegidio.name> - 2025-05-23 07:04 +0200
  Re: “Booleans Considered Harmful” David Brown <david.brown@hesbynett.no> - 2025-05-22 13:51 +0200
    Re: “Booleans Considered Harmful” Julio Di Egidio <julio@diegidio.name> - 2025-05-22 14:43 +0200
      Re: “Booleans Considered Harmful” Julio Di Egidio <julio@diegidio.name> - 2025-05-22 14:57 +0200
  Re: “Booleans Considered Harmful” Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-05-22 12:27 -0700
  Re: “Booleans Considered Harmful” JJ <jj4public@outlook.com> - 2025-05-23 16:56 +0700
    Re: “Booleans Considered Harmful” Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-05-23 22:58 +0000
  Re: “Booleans Considered Harmful” Lawrence D'Oliveiro <ldo@nz.invalid> - 2025-05-23 22:48 +0000
  Re: “Booleans Considered Harmful” c186282 <c186282@nnada.net> - 2025-06-18 02:40 -0400

csiph-web