Live data from Hacker News

Don't use booleans (2019)

luu.io

21–30 of 152 posts

Re: Don't use booleans (2019)

#21

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?

Oh gawd yes they do!

Re: Don't use booleans (2019)

#22
post #14
post #5

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

Which is far more realistic than defining single-use enums all over the place. I don't know why more languages don't have named parameters...

Why is defining an enum "unrealistic"? It's at most 4 lines of very simple code to define an enum that replaces a boolean.

Re: Don't use booleans (2019)

#23

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).

Named parameters are easy enough to mimic in JS using objects and the spread operation

function foo({bar, baz}) { ... }

foo({bar: 1, baz: false})

Re: Don't use booleans (2019)

#24
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.

Re: Don't use booleans (2019)

#25

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 actually set up VSCode and probably other editors to show the parameter names inline with the values.

Re: Don't use booleans (2019)

#26

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).

JS/TS has a pretty widespread convention for "named parameters":

  function foo({x, y, z=1}) { ... }
It eschews positional parameters though, but makes passing aptly-named variables as arguments easy:

  const x = 1;  // Usually a complex computation instead.
  const y = 2 * x;
  return foo({x, y});

Re: Don't use booleans (2019)

#27

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).

I got tremendously sad when I watched Guy Steele's talk growing a language:

https://www.youtube.com/watch?v=_ahvzDzKdB0

It's one of the best programming talks I've come across. For 1998, all his points on growing a new programming language are spot on.

And then Java became the opposite of a lot of the goals he enumerated.

Re: Don't use booleans (2019)

#28

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).

Named parameters are easy enough to mimic in JS using objects and the spread operation function foo({bar, baz}) { ... } foo({bar: 1, baz: false})

Lemme be an absolute pedant and say that's destructuring, not spreading.

And correct me if I'm wrong, nothing worse than an incorrect pedant.

Re: Don't use booleans (2019)

#29

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…

One of the things that's often not considered with these kind of interfaces, is whether or not they're actually possible. For instance, arg3 might only be true if arg1 is also true. Or arg3 may not be false if both arg1 and arg2 are also false.

Using object arguments is a great start, and I think using enums can also be powerful (if you're using string literal types). But often times I reach for explicit interfaces in these situations. IE:

type FooScenario = { arg1: true; arg2: true; arg3: true };

type BarScenario = { arg1: false; arg2: true; arg3: false; }

type Scenario = FooScenario | BarScenario;

...etc

This provides semantic benefits in that it'll limit the input to the function (forcing callers to validate their input), and it also provides a scaffold to build useful, complete test cases.

Re: Don't use booleans (2019)

#30

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 actually set up VSCode and probably other editors to show the parameter names inline with the values.

Same for JetBrains IDEs.
Post reply on HN