HI version is available. Content is displayed in original English for accuracy.
Advertisement
Advertisement
⚡ Community Insights
Discussion Sentiment
46% Positive
Analyzed from 1475 words in the discussion.
Trending Topics
#true#arguments#parameter#named#boolean#code#false#user#isadmin#https

Discussion (39 Comments)Read Original on HackerNews
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?
Also, obviously bot/bought account.
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.
And while searching for a reference, I found the original Qt Quarterly, archived here: https://doc.qt.io/archives/qq/qq13-apis.html - I am sure some of it is just hopelessly outdated, but the general insights probably still hold up. There's a probably-more-modern successor here on their wiki: https://wiki.qt.io/API_Design_Principles
Avoid the Long Parameter List
https://testing.googleblog.com/2024/05/avoid-long-parameter-...
As author points out: "So I’ll usually just make it explicit:
createAdminUser(user);
createRegularUser(user);
Now there’s not much left to interpret. To be fair, this isn’t always bad. Sometimes this is completely fine:
toggleMenu(true);
That’s clear enough. This tends to work when:
- the meaning is obvious
- the function is small and local
- there’s only one flag "
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)
Ultimately I think I’d bias towards readability vs the marginal perf increase though.
1. Avoid long parameter lists [1]
2. Avoid boolean arguments [2]
For #1, long parameter lists should be named (named arguments, options object, etc).
For #2, and booleans should be replaced by meaningful enumerations.
> toggleMenu(true); That’s clear enough. the meaning is obvious
Actually, it's incredible ambiguous. Is that toggling the menu? Or setting it to the open state? Or something else? I have no idea.
[1] https://testing.googleblog.com/2024/05/avoid-long-parameter-...[2] https://alexkondov.com/should-you-pass-boolean-to-functions/
CreateUser(CreateAdmin::enable);
> 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
https://kotlinlang.org/docs/functions.html#named-arguments
1. Somewhat often, parameter hints just stop working. Why? Who knows! There never seems to be any way to debug these mechanisms. They just stop working and you're forced to do stuff like delete random cache folders or toggle random options, as suggested by randos from Stack Overflow posts in 2017, until it maybe starts working again (for now)
2. Parameter hints sometimes aren't available when you need them. Xcode, for example, only seems to be able to put them inline, as text in the document that you replace with the arguments, something available only when writing code. You can't get them to pop up on demand as a reader as you can with, say, Visual Studio
3. You might be examining the code in some kind of review UI, rather than a code editor, in which case you're going to have to do some back and forth. You might be right in the middle of something yourself, and not in a position to retrieve the code locally to examine it in the usual editing environment. (I don't think you should go overboard optimising for this case, but I think it worth considering)
4. From the type checker perspective, all bools are equivalent. If you pass "false" for one bool parameter, then you could just as well pass that same "false" for another one - complicating rearranging or adjusting parameters, because, depending on the type of change you make, you may not get a good set of diagnostics to work through. And it gets worse if you've got defaulted or optional parameters
fwiw, while inlay hints are great, they don't work in either git-delta or github, so they're not availabe in a good chunk of the places that I'm looking at code, so for TypeScript, I do lean towards object arguments with keys the way the article suggests.