News for hackers. Stuff that matters.
Re:Don't Use Booleans by the__alchemist on Monday July 08, 2024 @07:02PM (#40910700)
You can think of booleans as a built-in, two-value enum that has a common semantic meaning. Use booleans if this is suitable for your purpose; it will be suitable for many.

That said... this article feels like a bit of a strawman. Does anyone by default use three booleans in cases that are arechetypical enums?

Re:Don't Use Booleans by makeitdouble on Monday July 08, 2024 @07:19PM (#40910832)
It usually starts with one boolean.

Then people avoid touching it when expending a bit, so they just add another boolean for the corner case they're dealing with (potentially as an optional last param). And so on, until someone refactors the whole function bearing the burden to retouch all its legacy that accumulated up to that point.

Re:Don't Use Booleans by asddubs on Monday July 08, 2024 @07:23PM (#40910853)
It usually starts with "is_car", a simple, clear, yes or no. and then at some point someone needs to also check if it's a sedan and instead of turning it into an enum called vehicle_type an "is_sedan" is added, etc.
Re:Don't Use Booleans by PhilipRoman on Tuesday July 09, 2024 @06:55AM (#40914596)
The other side of the coin is making an enum and eventually realizing that some of the types can overlap. I prefer the "set of tags" approach due to its flexibility.
Re:Don't Use Booleans by griftrejection on Tuesday July 09, 2024 @03:39PM (#40920137)
Can't developers just... not do that? You can write bad code in any language. What is our responsibility as experts to avoid doing things like this, then?
Re:Don't Use Booleans by asddubs on Tuesday July 09, 2024 @05:59PM (#40921696)
if they know better and have the foresight, they do. But stuff like this might not occur to people who've never been bitten by it. Every step of the way you just make things a little bit messier than before.
Re:Don't Use Booleans by pavel_lishin on Monday July 08, 2024 @07:19PM (#40910828)
My friend, I'd love to give you a tour of our codebase.
Re:Don't Use Booleans by thomasingalls on Monday July 08, 2024 @07:05PM (#40910726)
Unfortunately, yes
Re:Don't Use Booleans by replyifuagree on Monday July 08, 2024 @07:13PM (#40910789)
Oh gawd yes they do!
Re:Don't Use Booleans by frou_dh on Tuesday July 09, 2024 @05:39AM (#40914131)
> You can think of booleans as a built-in, two-value enum that has a common semantic meaning.

I love it when a language's bool type is just a sum-type like the sum-types you define in user code, e.g. https://ocaml.org/manual/5.2/core.html#hevea_manual10. It's an indicator of a language with good foundations.

Re:Don't Use Booleans by xigoi on Tuesday July 09, 2024 @01:31PM (#40918707)
I agree. Nim’s booleans are an enum too: https://nim-lang.org/docs/system.html#bool