Live data from Hacker News

Don't use booleans (2019)

luu.io

61–70 of 152 posts

Re: Don't use booleans (2019)

#61
post #5

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.

[deleted]

Re: Don't use booleans (2019)

#63
post #62

The 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 :(

Fortunately you can use an options struct which retains most of the benefits of named parameters

Re: Don't use booleans (2019)

#64

Downsides: 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.

The IDE, like IntelliJ, can tell you which boolean has what name without named parameters.

Re: Don't use booleans (2019)

#65

Tell me you're almost ready for Rust without telling me you're almost ready for Rust.

You're not wrong, but you're probably getting downvoted because you didn't elaborate on what you mean. Possibly also because the way Rust handles structs isn't exactly unique to rust. Go does it, Typescript does it, and so on.

Re: Don't use booleans (2019)

#66
post #33

Earlier 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

Never had an issue with “remembering” part. At first I hated js/ts for its runtime and legacy quirks, but shortly after getting used to them it struck me that it’s a practical language with as little religion as possible. They just look at something and make it useful. Without thinking how much horrible it could be if they only imagined it.

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)

#67
Tangentially -- 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 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

[1]: https://wiki.haskell.org/Type

Re: Don't use booleans (2019)

#68
post #5

Solution in C#: use named parameters: RaiseInvoice(taxInvoice: false, sendEmail: true)

You can't force the caller into using named parameters though. So when it's late at night, you're tired but also think you know better, there's nothing stopping you from doing RaiseInvoice(true, false) when you actually meant the inverse.

Re: Don't use booleans (2019)

#69

Tangential 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…

you can force named arguments in python by using * after any non-named args. example:

    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 given

Re: Don't use booleans (2019)

#70

Tangentially -- 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…

Typescript is great for this.

    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.
Post reply on HN