Solution in C#: use named parameters: RaiseInvoice(taxInvoice: false, sendEmail: true)
If you don't have named parameters, you can fall back to variables var taxInvoice = false var sendEmail = true RaiseInvoice(taxInvoice, sendEmail) It's obvious, but people tend not to do it.
Don't use booleans (2019)
61–70 of 152 posts
Re: Don't use booleans (2019)
#62The assumption being that the language you are using does not have named parameters. Something all modern languages have, except, as usual, the ones the world is built on (JS, Java).
Re: Don't use booleans (2019)
#63The assumption being that the language you are using does not have named parameters. Something all modern languages have, except, as usual, the ones the world is built on (JS, Java).
And Go :(
Re: Don't use booleans (2019)
#64Downsides: you have to name and import all the enums (notice no one ever shows this in code examples). You can't do boolean math on them (and, or, etc.).
In most languages with strong types, the IDE will handle imports for you pretty easily. Selecting an import with a hotkey isn't as hard as debugging a mysterious boolean. Not being able to use logical operations on them is a feature, not a bug: logical operations on unnamed values here are going to be even less readable than passing them into functions.
Re: Don't use booleans (2019)
#65Tell me you're almost ready for Rust without telling me you're almost ready for Rust.
Re: Don't use booleans (2019)
#66Earlier quoted context omitted.
Named parameters are easy enough to mimic in JS using objects and the spread operation function foo({bar, baz}) { ... } foo({bar: 1, baz: false})
that's its own kind of horrible though. now either all your functions just take single object parameters or you have to remember which ones. and I get javascript is light on checking what was passed is actually what was expected but this seems to somehow make that situation even worse still
The ([…, ]{…}) approach is called “options object” which is a separate entity and is very convenient compared to e.g. python’s semi-infinite arguments lists.
Re: Don't use booleans (2019)
#67Well you knew what was coming -- Rust enums are excellent[0], and so are Haskell's[1] (try and spot the difference between an enum and a record type!)... But that probably won't help you at $DAYJOB.
A bit more on topic though -- I'd like to see a strong opinion on Option versus SomeEnum containing a Missing variant. I usually lean towards Option but I wonder if there are any devout zero value proponents.
I don't like how golang does/requires zero values, but in more expressive languages I do waver sometime between Option and T w/ a "missing" indicator variant inside.
[0]: https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html
Re: Don't use booleans (2019)
#68Solution in C#: use named parameters: RaiseInvoice(taxInvoice: false, sendEmail: true)
Re: Don't use booleans (2019)
#69Tangential 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 carr…
In [1]: def my_func(first, *, second, third):
...: print(first, second, third)
...:
In [2]: my_func(1, second=2, third=3)
1 2 3
In [3]: my_func(1, 2, 3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 my_func(1, 2, 3)
TypeError: my_func() takes 1 positional argument but 3 were given
In [4]: def my_func_2(*, first, second, third):
...: print(first, second, third)
...:
In [5]: my_func_2(first=1, second=2, third=3)
1 2 3
In [6]: my_func_2(1, second=2, third=3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[6], line 1
----> 1 my_func_2(1, second=2, third=3)
TypeError: my_func_2() takes 0 positional arguments but 1 positional argument (and 2 keyword-only arguments) were givenRe: Don't use booleans (2019)
#70Tangentially -- use languages that have great enum support. Well you knew what was coming -- Rust enums are excellent[0], and so are Haskell's[1] (try and spot the difference between an enum and a record type!)... But that probably won't help you at $DAYJOB. A bit more on topic though -- I'd like to see a strong opinion on Option versus SomeEnum containing a Missing variant. I usually lean towards Option but I wonder…
function operation(user: User, state: “active” | “inactive”): void
Boom. Done. Enum defined. You want people to use enums? Remove all context switching from the definition process.