Ultimately I think I’d bias towards readability vs the marginal perf increase though.
I feel like a goal with good code is localizing understanding even if it occasionally duplicates something like a parameter name.
const isAdmin = true; . . . createUser(user, isAdmin, sendWelcomeEmail)
> And I’ve seen real calls like this in production code: > updateSettings(user, true, false, true, false)
Really? He wants named parameters on all function calls cos he's got a memory like a sieve? This is a long solved problem to me
createUser(user, ~isAdmin:true, ~sendWelcomeEmail:false)
Even though in OCaml's functional style it is actually like this: createUser user ~isAdmin:true ~sendWelcomeEmail:false
Using the fact that a variable named exactly like a labeled argument is automatically assigned to it, we can make the call more concise (especially if reusing existing variables): let isAdmin = true in
let sendWelcomeEmail = false in
createUser user ~isAdmin ~sendWelcomeEmail > function foo({a, b, c}) {
... return {x: a, y: b, z: c};
... }
undefined
> foo({a: 1, b: 2, c: 3})
{ x: 1, y: 2, z: 3 }
> const a = 'A', b = 'B', c = 'C';
undefined
> foo({a, b, c})
{ x: 'A', y: 'B', z: 'C' }
>Avoid the Long Parameter List
https://testing.googleblog.com/2024/05/avoid-long-parameter-...
Second, this may differ a bit from language to language, but maybe those booleans should not be a boolean: https://gleam.run/documentation/conventions-patterns-and-ant... for example isAdmin boolean could instead be a UserRole custom type, with variants Normal and Admin, which is easier to understand in the function call, and extendable with another Moderator (or whatever) variant
so... it does toggle the menu? and toggleMenu(false) doesn't toggle it and keeps it as it is?
or is it toggle extended menu vs toggle basic menu?
PaulKeeble•1h ago
I don't mind the object approach used here but its quite verbose in comparison even in Javascript. Having to name the variable and set whether its true or false is a lot more than needs to be done. Booleans in general have quite poor readibility and maintenance especially if a third possibility arrives.