3 ms·
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 suitab
by the__alchemist 2y ago
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?
- thomasingalls 2y agoUnfortunately, yes
- replyifuagree 2y agoOh gawd yes they do!
- pavel_lishin 2y agoMy friend, I'd love to give you a tour of our codebase.
- makeitdouble 2y agoIt 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.
- asddubs 2y agoIt 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.
- PhilipRoman 2y agoThe 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.
- griftrejection 2y agoCan'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?
- asddubs 2y agoif 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.
- frou_dh 2y ago> 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 https://ocaml.org/manual/5.2/core.html#hevea_manual10. It's an indicator of a language with good foundations.
- xigoi 2y agoI agree. Nim’s booleans are an enum too: https://nim-lang.org/docs/system.html#bool https://nim-lang.org/docs/system.html#bool