Don't use booleans (2019)
1–10 of 152 posts
Re: Don't use booleans (2019)
#2Folks, the headline is real bad and clickbaity, but I think the blog post is actually very, very correct in its basic premise.
Re: Don't use booleans (2019)
#3Re: Don't use booleans (2019)
#4I'm definitely a fan of making bad data 'unrepresentable' through the type system. The bit flag enum pattern is not so familiar to me in Python, but I recognize it from the CPython internals so maybe it's more common in systems languages. Not sure how I feel about explicitly listing every valid combination of flags, but I'm sure in some cases that's the right move.
Re: Don't use booleans (2019)
#5Solution in C#: use named parameters: RaiseInvoice(taxInvoice: false, sendEmail: true)
Re: Don't use booleans (2019)
#6[deleted]
Re: Don't use booleans (2019)
#7Tangential but coding in JS/TS, I often will go for object arguments to make things more readable. If you have a function like:
foo(arg1: boolean, arg2: boolean, arg3: boolean)
Then when you call it it will look like foo(true, false, true) which is not great for readability. Instead I move all the arguments into an object which makes each field explicit. Ie.
foo({ arg1: true, arg2: false, arg3: true })
This also carries the advantage that you can quickly add/remove arguments without going through an arduous refactor.
Re: Don't use booleans (2019)
#8Keyword arguments or named parameters solve this. In JS I tend to pass a single object to a function with a large number of parameters like this. But I do agree using named entities like enums or constants is also good. A bit heavier but if you do it everywhere the cost is worth it.
Re: Don't use booleans (2019)
#9You 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 (2019)
#10Also called the Boolean trap: https://ariya.io/2011/08/hall-of-api-shame-boolean-trap