News for hackers. Stuff that matters.
Re:Get rid of those boolean function parameters (2015) by drath on Monday September 20, 2021 @06:29AM (#28592194)
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})
Re:Get rid of those boolean function parameters (2015) by dugmartin on Monday September 20, 2021 @06:40AM (#28592250)
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.
Re:Get rid of those boolean function parameters (2015) by seanwilson on Monday September 20, 2021 @08:08AM (#28592894)
> 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)?

Re:Get rid of those boolean function parameters (2015) by kevincox on Monday September 20, 2021 @12:06PM (#28595328)
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.