Live data from Hacker News

Get rid of those boolean function parameters (2015)

mortoray.com

61–70 of 119 posts

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

#61
post #39

Named parameters misses the point. Functions should do one thing and only one thing. This is why we have cos, sin, and tan and not a universal function for trigonometry: trig(mode='cos', ...). Such functions often become cumbersome to use since they are essentially multiple functions baked into one.

function pizza(boolean pepperoni, boolean bacon, boolean mushroom, boolean artichoke) now becomes 16 distinct functions.

[deleted]

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

#62
post #7

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

Although, it doesn't always. For example,

calc_formula(1,2,true)

would show as

calc_formula(a: 1, b: 2, is_gain: true)

but

calc_formula(1,2,some_var)

shows as

calc_formula(a: 1, b: 2, some_var)

Although on the flip side, it creates an incentive for the developer to choose sensible names for variables, functions etc.

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

#63
post #33

Earlier quoted context omitted.

Fwiw, Ada does this too.

Does this feature have a general name? Swift users seem to call it "dot syntax", which is not a good name. I want to google "C++ should have " and find a proposal from 2013 that never moved forward, but "C++ should have dot syntax" is just silly.

The official Swift name of the syntax is “implicit member expression”.

https://docs.swift.org/swift-book/ReferenceManual/Expression...

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

#64

In Delphi I try to use sets over boolean parameters. Then I can easily add new possible set members instead of introducing more parameters. type FuncParam = (fpDoThis, fpDoThat); type FuncParams = set of FuncParam; function MyFunc(arg1, arg2: integer; params: FuncParams): integer; begin result := 0; if (fpDoThis in params) then result := DoThis(arg1); ... end; // supply directly MyFunc(123, 42, [doThis, doThat]); //…

Delphi has overload and default parameters. The problem in article is non existent for a good Delphi programmer.

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

#65
post #7

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

Except this isn't an issue that should be solved at the IDE level. Not everybody is using the same IDE and has all the same features, or even the same options enabled in an IDE.

The solution provided in the article is the way to go.

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

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

Code is for humans, not for computers.

Choose:

  AddElement(object, true, false);
  AddElement(object, true, true);
  AddElement(object, false, false);
  AddElement(object, false, true);

or

  AddElement(object, visible::on, deletable::off);
  AddElement(object, visible::on, deletable::on);
  AddElement(object, visible::off, deletable::off);
  AddElement(object, visible::off, deletable::on);
The latter is more readable, you can spot bugs easier, you don't need to remember which parameter was for visibility, and which was for indicating deletable. And it doesn't take much more to write this than a confusing boolean. It doesn't scale.

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

#67
post #41

Earlier quoted context omitted.

Religiously splitting functions with boolean arguments doesn't always result in more maintainable code. Instead of trigonometry functions, how would you refactor JS's fetch() with many of its behaviour-altering flags?

> how would you refactor JS's fetch() as @flavius29663 said ( https://news.ycombinator.com/item?id=28593669 ) you can use the builder pattern FetchBuilder() .withUrl(ur) .withMode("cors") .withCache(true) .withHeader('Content-Type', 'application/json') .accept('*/*') .post() .then(response => response.json()) .then(data => console.log(data));

I see the builder pattern as a way to manage lack of keyword arguments. I see very little difference between your example and the actual fetch API that takes an object as JS's version of keyword arguments.

Languages with good support for named/keyword arguments have more features such as required arguments and preventing duplicate arguments. With builder patterns your only real option is to make the builder constructor have required arguments (or throw a runtime error upon finalizing the builder).

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

#68
post #66

Earlier quoted context omitted.

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

Code is for humans, not for computers. Choose: AddElement(object, true, false); AddElement(object, true, true); AddElement(object, false, false); AddElement(object, false, true); or AddElement(object, visible::on, deletable::off); AddElement(object, visible::on, deletable::on); AddElement(object, visible::off, deletable::off); AddElement(object, visible::off, deletable::on); The latter is more readable, you can spot…

[deleted]

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

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

I forget which version it starts in, but in newer python’s you can have keyword only arguments (they can’t be positional like in your example)

    def calc_formula(first, second, *, is_gain=False):
    …

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

#70

Earlier quoted context omitted.

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

In theory yes. But for hot inner loops you will probably get it optimized away. (This is a common pattern so optimizer try to find it and undo it. Furthermore hidden classes for objects are common and when you destructure directly in the argument list escape analysis is pretty easy) It probably does hurt your performance for warm and cold code but that likely isn't too significant.

So yes, if performance is critical you should probably profile and consider avoiding this pattern, but for most cases the performance impact is very minor.

Post reply on HN