Live data from Hacker News

Get rid of those boolean function parameters (2015)

mortoray.com

21–30 of 119 posts

Re: Get rid of those boolean function parameters (2015)

#21

As pointed out in some of the article's comments, a much better solution would be if all languages allowed putting the name in front of function parameters (and while at it, also not enforce a specific parameter order and skip default-value parameters). A workaround in C99 (and more limited in C++20) is to use a single struct which bundles all the function parameters, and then use designated initialization, this also…

Many newer languages support named function arguments: Swift, Kotlin, etc.

Re: Get rid of those boolean function parameters (2015)

#22
post #17

Earlier quoted context omitted.

While I think parameter names at all call sites is the right solution, I don't think it's good enough to have this in the IDE alone. I still have to review code online that says `add(a, true)`, even if IntelliJ showed me `add(a, ignore_negatives:true)`.

The point is that this is a presentation issue. There is nothing wrong with the model. It would not be impossible for the reviewing software to analyze source code and display named parameters.

OTOH, programming for the human and not for the computer, included caring about presentation. I could say there's nothing right with the model either - a non-issue, personal preference.

... An older presentation issue is the tabs versus spaces flamewar. At least that one has burnt out. Maybe because of the rise of IDE's?

Re: Get rid of those boolean function parameters (2015)

#23
post #17

Earlier quoted context omitted.

While I think parameter names at all call sites is the right solution, I don't think it's good enough to have this in the IDE alone. I still have to review code online that says `add(a, true)`, even if IntelliJ showed me `add(a, ignore_negatives:true)`.

The point is that this is a presentation issue. There is nothing wrong with the model. It would not be impossible for the reviewing software to analyze source code and display named parameters.

That would require every bit of software that presents this program not only to understand my programming langauge, but also to have enough context on the rest of the program to actually recognize the function and know what each parameter represents.

Not to mention, code is itself a presentation layer. Why would you put some presentation concerns in one layer (e.g. identifier names, indentation&styling), but others in another presentation layer?

Re: Get rid of those boolean function parameters (2015)

#24
There are a couple ways to approach it in Common Lisp, probably a few more than I have here.

One, optional and keyword arguments can have defaults.

  (defun calc-formula (ia ib &optional (gain t))
    ...)
Two, you could use a dynamic variable and a closure. Outside the body of the let the gain var returns t, within the body of the let it returns nil.

  (defvar *gain-enabled* t)

  (defun calc-with-gain (ia ib)
    ...)

  (let ((*gain-enabled* nil))
    (defun calc-without-gain (ia ib)
      ...))

Re: Get rid of those boolean function parameters (2015)

#25

In Erlang, one would usually pass atoms like 'with_gain' or 'without_gain', and substitute default values with arity-overloading: e.g. there would be two calc_formula functions, one with 2 arguments and one with 3 arguments, and the 2-argument one would simply call the 3-argument with the last parameter set to 'with_gain'. And in case of really large number of parameters, one would generally pass either a map or (in…

> There is no beautiful syntax for destructuring all that stuff with default values, unfortunately.

Actually there is one: records with default values.

https://erlang.org/doc/programming_examples/records.html

Re: Get rid of those boolean function parameters (2015)

#27
post #3

In python, I love keyword-only arguments for this. Then the caller has to write: v = calc_formula(ia, ib, is_gain=true) You also have the option of defining a default value for the argument so the old call-sites don't even need modification.

In C++ I’ve gotten in the habit of `enum class is_gain : bool { no, yes };` so the call site is `v = calc_formula(ia, ib, is_gain::yes);`. An advantage is that such stron Boolean-like types can be passed repeatedly and maintain their type safety.

The 'require named argument' solution is less strong than the enum solution. (An enum is also available in python). Indeed re-use of the plain bool in Python is less clear, as is passing it on. This makes the enum the best solution.

However, enum is also a heavy-duty solution. It requires slightly more typing, but more importantly, it requires exporting an enum to all call-sites. Both in C++ and in python this is not desirable.

I'd say the enum should be in the toolbox, especially if the flag is important to the business logic of the code, and is likely to thread throughout it. But for quick work, a key-word only argument can work just as well. Especially if the flag is never to be passed on.

Re: Get rid of those boolean function parameters (2015)

#28
post #7

IntelliJ solves this nicely by showing the parameter name at call sites, effectively making it look like the language has named parameters.

Fully agree. No need to change the source code to fix what isn't broken.

That helps when you are writing the code. It's less helpful when you are modifying a function being called from all over the code base. Arguably smart enough refactoring tools will help, but smart enough compilers have been doing wonders for decades, too.

Re: Get rid of those boolean function parameters (2015)

#29
post #17

Earlier quoted context omitted.

The point is that this is a presentation issue. There is nothing wrong with the model. It would not be impossible for the reviewing software to analyze source code and display named parameters.

That would require every bit of software that presents this program not only to understand my programming langauge, but also to have enough context on the rest of the program to actually recognize the function and know what each parameter represents. Not to mention, code is itself a presentation layer. Why would you put some presentation concerns in one layer (e.g. identifier names, indentation&styling), but others i…

> every bit of software that presents this program not only to understand my programming language

You can either change your program to fit existing tools, or you can build smarter tools. I prefer the latter.

> code itself is a presentation layer

Not for the tool it isn't

edit: I think we can all agree that ideally we fix this in the language itself by adding optional named parameters

Re: Get rid of those boolean function parameters (2015)

#30
post #6

This should really be solved by using named parameters or by writing docblocks so that IDE can show hints. Another trick, at least in js, is to use destructuring assignment, e.g. function calc_formula({a, b, is_gain}){ ... } calc_formula({a:1, b:2, is_gain:true})

My rule of thumb these days in JS/TS is all functions with more than 2 parameters should be refactored to a single object parameter using destructuring. I don't start with a single object because most times YAGNI applies.

> My rule of thumb these days in JS/TS is all functions with more than 2 parameters should be refactored to a single object parameter using destructuring.

Does this create garbage for the garbage collector (which might be an issue for inner loops)?

Post reply on HN