Sujet : Re: “Booleans Considered Harmful”
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.programmingDate : 22. May 2025, 12:51:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <100n34e$3eqip$1@dont-email.me>
References : 1
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 22/05/2025 08:51, Lawrence D'Oliveiro wrote:
I think most of this article is a load of nonsense, myself.
<https://www.infoworld.com/article/3990923/booleans-considered-harmful.html>
Thoughts?
He makes some relevant points about clarity of code, but most is general and unrelated to booleans, and a lot of what he writes is exaggerated.
So in general, I'd prefer positive names to negative ones - "UserIsAuthorised" rather than "UserIsNotAuthorised". And I would not "hide" a "not" in the middle of a variable name as his example does. But sometimes a negative name makes more sense - "UserBanned" might be perfectly reasonable.
All things being equal, I'd usually choose "put positive first". But again, that's more a bias than a rule. In particular, it might often be best with an "early exit" coming first regardless of whether it is the positive condition or the negative condition.
And while I agree with the principle of "no complex expressions", I think he is exaggerating what makes an expression complex, and picking a poor way to handle it. Some parentheses can make a huge difference to readability without needing to introduce extra names. Sometimes nested if's are a good way to split complicated conditionals. Adding extra names is a good idea only if they add clarity to the code - the writer's examples do not. And yes, people /will/ complain about absurdly long variable names.
Excessive boolean parameters in functions are bad - but so are excessive parameters of any other type.