3 ms·
I’ve used TypeScript and VSCode for years and I still find this infuriating: const anything = (a: ActualTypeA, b: ActualTypeB, someOptionalFlagIveInspected
by eyelidlessness 5y ago
I’ve used TypeScript and VSCode for years and I still find this infuriating:
const anything = (a: ActualTypeA, b: ActualTypeB, someOptionalFlagIveInspectedTenThousandTimesAndStillForget?: boolean) => IrrelevantReturnType
What does the parameter mean? Is it in the doc block? (Almost never.) Does it default to false? (Now you’re reading the whole potential call stack.) Are additional behaviors associated with its absence or trueness that aren’t inferred at the call site? (Who knows but it’s a fair roll of the dice!)
- ajxs 5y agoWhat's infuriating about this? If you give those variables good, descriptive names, and provide a default argument for the optional variable (which is just basic good programming practice), then what's the problem?
- eyelidlessness 5y agoThe problem is none of those things are visible where they matter. Sure your function signature might say what “true” means, and what parameter “true” is assigned to. Your IDE will probably even give you hints as you type. Those won’t show up in code review. They won’t show up in diffs at all if you rename the parameter and change its semantics.
- heavyset_go 5y agoWhy would you make a boolean parameter also an optional?
- eyelidlessness 5y agoIt’s really common in JS. Typically the default is false, but an unreasonably large minority including some really notable cases default true… which makes it that much worse.
- Zababa 5y agoYou write a function that does one thing, and then you realise it can be used for something else that's almost the same thing, so you add an optional boolean parameter.
- tengbretson 5y agoI can't see how passing it in as an object property fixes any of those issues.
- eyelidlessness 5y agoYou can unconditionally see what it is at the call site.
- Aeolun 5y agoIf you give your flag such a name you’ll never know what it does.