4 ms·
Having programmed in languages with named parameters (Smalltalk, ObjC and Commonlisp) I’ve found them cumbersome. Also they can lead to a poor pattern which is
by gumby 6y ago
Having programmed in languages with named parameters (Smalltalk, ObjC and Commonlisp) I’ve found them cumbersome.
Also they can lead to a poor pattern which is functions/methods that take too many parameters.
They also tend to make generics harder to understand as the possible parameters can vary. Of course you can make the same problem in C++ via, say, `auto multiply(int x, int y)` and `auto multiply(double x, double y, enum rounding rp = rounding::nearest)`
In general they impose the verbosity (in keyboarding and reading) in the routine cases but don’t free you from having to look up the argument semantics of lesser-used functions.
Side point: I used “keyboarding” because the obvious word, “typing” has a homonym which made the sentence confusing!
- rietta 6y agoI use the equivalent of named parameters in Ruby as my preferred method for a few reasons: 1. Clearly communicates the purpose of the variable 2. Means that order of parameters is not important 3. Makes the internal interface largely compatible with its JSON web service interface It's been about 10 years since I coded any Objective-C and I do agree that syntax was an oddbal.
- stephenhuey 6y agoYes, I’ve used it in Ruby and now I find myself using it a lot in Dart. Also, the Flutter widgets use it and I find it much easier to figure out which arguments to fill in with my IDE instead of always jumping over to the documentation.
- vlovich123 6y agoI found it quite useful & more natural to read in Objective-C/Swift & in those you typically are writing in an IDE that auto-completes all the names. I've also not generally found them cumbersome in Python where I code primarily in VIM without auto-completion. YMMV
- ape4 6y agoOn no! Something in C++ that can be misused ;)
- zelphirkalt 6y agoWait, we can fix it by adding another syntax in C++22!
- deleted 6y ago[deleted]
- imglorp 6y agoKotlin has a nice convention with named and variable arguments. If the types are all different, it's not ambiguous and the compiler can figure it out, but you're free to name them at the call site anyway. I really don't see how we've suffered the C convention for 40 years when we could have had this instead. https://kotlinlang.org/docs/reference/functions.html https://kotlinlang.org/docs/reference/functions.html
- benrbray 6y agoPython has a similar convention with args/kwargs, and I find it massively helpful when trying to become situated in a codebase I didn't write myself.
- mlthoughts2018 6y agoIt’s also critical in Python to make heavy use of metaprogramming with decorators, so the bundling / unbundling operators combined with named parameters aren’t just for ease of reading or annotating call sites, but rather they open up a huge range of behavior that can only work if decorators can easily manipulate parameters of the functions they are decorating. For example, suppose you have a function foo and within foo you call another function baz, where baz has a very large range of customization parameters. If you want to memoize calls to foo with a decorator, you can just pass kwargs for any “pass through” customizations of baz. The memoizing decorator can flatten kwargs out into a tuple along with any positional args, so you get call signature memoization without needing to restrict or list out all possible args of baz. In fact you can make this perfectly generic over the function being memoized so it doesn’t matter if you change baz to bar one day, with a whole new signature. You can combine this with the inspect module and with functools.wraps to ensure full and complete docstrings and introspected signatures as well, so that putting kwargs all over doesn’t hurt readability, remove info from docstrings or help messages, or make it hard to read.
- benrbray 6y ago> It’s also critical in Python to make heavy use of metaprogramming with decorators Interesting use case, thanks for sharing! Out of curiosity, when are some scenarios where it's "critical" to write code this way? I mostly interact with small pytorch / numpy codebases that don't grow large enough for this sort of thing.
- mucholove 6y agoHmm. Why do you think they would lead to misuse via functions that take too many parameters? I understand how this is a problem but I’ve seen more than one function in C that takes 4 or more parameters. Methinks it’s more a product of being an inexperienced coder and not being able to model a domain appropriately. The huge benefit? If you do need 4 parameters you can at least read them / understand them if they are named! Would love to see you flesh this out a bit more
- gumby 6y agoWell, my example was a small case of this problem. It’s not unreasonable that when operating on floating point, different algorithms can need different ways of determining which FP number to choose when the result cannot be precisely represented. And the example I gave is innocuous...though it’s likely an underlying function called by functions that implement higher level semantics. And in fact in that case using a keyword isn’t that different from binding the meaning into the function name (`divide_rounding_down` and so on). But in my experience I saw a pernicious pattern: that the function name would essentially become the common entry point for a large number of divergent functions `divide(dividend: x, divisir: y, truncate: true, underflow_handling: ufh::throw, negative_permitted: true)` and so on (contrived example for explanatory purposes). In addition, positional arguments are simply easier to read; as keyword arguments are not ordered, you’re essentially parsing the arguments, seeking the important ones etc. You’re increasing the cognitive overhead of the common case while only marginally assisting the uncommon case. It’s like reading while hearing the sound of the words in your head: quite possible, but significantly slower than having the meaning enter directly.
- mucholove 6y agoNot a minor nit: in Objective-C, keyword arguments are ordered. Every function call reads like a sentence and the keyword argument itself can be something phrase like: -onStream:writeObject:withKey:;
- jasode 6y ago>Also they can lead to a poor pattern which is functions/methods that take too many parameters. >In addition, positional arguments are simply easier to read; as keyword arguments are not ordered, you’re essentially parsing the arguments, seeking the important ones etc. Microsoft's raw Win32 API SDK is based on the C Language and even though that language doesn't have named parameters, it still did not discourage functions designed with lots of parameters that's difficult to mentally parse. To make such code self-documenting, this is how I typically write Win32 API calls to make the code readable: ret = CreateProcess(NULL, // _In_opt_ LPCTSTR lpApplicationName, cmd, // _Inout_opt_ LPTSTR lpCommandLine, NULL, // _In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes, NULL, // _In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, TRUE, // _In_ BOOL bInheritHandles, CREATE_NO_WINDOW, // _In_ DWORD dwCreationFlags, NULL, // _In_opt_ LPVOID lpEnvironment, NULL, // _In_opt_ LPCTSTR lpCurrentDirectory, &si, // _In_ LPSTARTUPINFO lpStartupInfo, &pi); // _Out_ LPPROCESS_INFORMATION lpProcessInformation If the language had named parameters, I wonder if Microsoft would have used them so programmers wouldn't have to sprinkle extra comments like that. (Comments are also brittle because they're not real tokens that the compiler would check.) Without the comments, it looks like this: ret = CreateProcess(NULL, cmd, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi); All those naked NULLs and TRUE params are very hard to parse unless you've memorized the positions. I'm not recommending that we need the verbosity of Objective-C named parameters but I don't see how mentally relying on position is less cognitive load.
- coldtea 6y ago>Also they can lead to a poor pattern which is functions/methods that take too many parameters. Never seen that as a tendency in languages with named params. I have, however, seen functions / methods in languages without named params that take too many arguments and one has to guess what they are...
- Gibbon1 6y agoPassing structs to functions is bad bad/Creates functions with 14 parameters instead.
- kllrnohj 6y agoWhat named parameters fixes is making constant parameters readable. You can API design around this (see for example https://wiki.qt.io/API_Design_Principles#The_Boolean_Parameter_Trap https://wiki.qt.io/API_Design_Principles#The_Boolean_Paramet... ). You can also always use local constants. But those are non-trivial or cumbersome in their own way, and often people don't for their own internal functions. Named parameters neatly side-steps the issue entirely, by rolling the "local constant" right into the function call itself. Like consider these two python snippets: ['Ford', 'BMW', 'Volvo'].sort(true) vs. ['Ford', 'BMW', 'Volvo'].sort(reverse=true) The latter is much more readable than the former, and there wasn't any "too many parameters" issue. It's a way to help create self-documenting code that's also compiler enforced. And it absolutely frees the reader up from needing to go lookup what the boolean parameter on "sort" means.
- koyote 6y agoAlthough I agree in general, I think that most of the time this should be solved on the API level (like your link suggests). This is because although you 'can' name your parameters, there is nothing forcing you to. This means that many people forget or are too lazy, so you still end up with a large part of your code not being as readable as it could be. For the above you'd have an 'rsort' or 'reversesort' function instead (or if your language supports it, some fluent kind of sort().reverse()). If you have a lot of options, some of them boolean, you might send in an options struct or have an enum as mentioned in the article.
- vore 6y agoAnother pattern especially for ambiguous Boolean parameters I also find helpful is something like (in pseudo-ish code): enum SortDirection { FORWARD, REVERSE } sort(direction : SortDirection = SortDirection.FORWARD) { ... } ['Ford', 'BMW', 'Volvo'].sort() ['Ford', 'BMW', 'Volvo'].sort(SortDirection.REVERSE)
- kllrnohj 6y agoYup, that's the API design tweak that QT recommends as well. But it's a lot more boilerplate, and that sort of boilerplate basically never happens for internal-only methods. Named parameters is basically the middle ground solution for the "quick" hacks that have a tendency to be rather permanent hacks.