The conversation

Lessons learnedExperience excerpt · English

A piece of advice I read somewhere early in my career was "a boolean should almost never be an …

A piece of advice I read somewhere early in my career was "a boolean should almost never be an argument to a function". I didn't understand what the problem was at the time, but then years later I started at a company with a large Lua code-base (mostly written by one-two developers) and there were many lines of code that looked like this: serialize(someObject, true, false, nil, true) What does those extra arguments do? Who knows, it's impossible without looking at the function definition. Basically, what had happened was that the developer had written a function ("serialize()", in this example) and then later discovered that they wanted slightly different behaviour in some cases (maybe pretty printed or something). Since Lua allows you to change arity of a function without changing call-sites (missing arguments are just nil), they had just added a flag as an argument. And then another flag. And then another. I now believe very strongly that you should virtually never have a boolean as an argument to a function. There are exceptions, but not many.

Original discussionThat boolean should probably be something else136 source comments · at import
8

Community replies 0

No community replies yet. Add your perspective.

Replies from the source 8

Direct replies available in this import. Original-language excerpts.

AR

If you use keyword arguments then something like that doesn't look too bad: serialize(someObject, prettyPrint:true) NB I have no idea whether Lua has keyword arguments but if your language does then that would seem to address your particular issue?

Full original
LE

It's a failing of many type systems of older languages (except Pascal). The best way in many languages for flags is using unsigned integers that are botwise-ORed together. In pseudocode: Object someObject; foo (someObject, Object.Flag1 | Object.Flag2 | Object.Flag3); Whatever language you are using, it probably has some namespaced way to define flags as `(1 << 0)` and `(1 << 1)` etc.

Full original
AC

But this isn't really a boolean problem - even in your example there is another mistery argument: nil And you can get the same problem with any argument type. What do the arguments in copy(obectA, objectB, "") mean? In general, you're going to need some kind of way to communicate the purpose - named parameters, IDE autocomplete, whatever - and once you have that then booleans are not worse than any other type.

Full original
NU

> I now believe very strongly that you should virtually never have a boolean as an argument to a function. There are exceptions, but not many. Really? That sounds unjustified outside of some specific context. As a general rule I just can't see it. I don't see whats fundamentally wrong with it. Whats the alternative? Multiple static functions with different names corresponding to the flags and code duplication, plus switch statements to select the right function? Or maybe you're making some other point?

Full original
ST

Named arguments are a solution to precisely this issue. With optional arguments with default value, you get to do precisely what was being done in your Lua code but with self documenting code. I personally believe very strongly that people shouldn’t use programming languages lacking basic functionalities.

Full original
MO

Something I really love in Elixir is that functions can be named identically and are considered different with different arity. https://elixirschool.com/en/lessons/basics/functions#functio...

Full original
VI

I die a little inside every time I write: JSON.stringify(val, null, 2); (So yes, but it goes beyond booleans. All optional parameters should be named parameters.)

Full original
DR

I'm surprised nobody has suggested this yet. Just use a different name for the function. In your example, the new function should be prettyPrint(). No booleans required. No extra structures required.

Full original
Continue on Hacker News

Respect different perspectives. Share concrete experiences.

Sign in to post, reply, repost, and quote.