Live data from Hacker News

Problems with default function arguments (2020)

quuxplusone.github.io

1–10 of 28 posts

Re: Problems with default function arguments (2020)

#2
Sure, the pitfalls are real. But they have benefits too. A fair assessment should examine both sides before coming to a decision, not just one. Off the top of my head, these come to mind:

- Defaults avoid having to duplicate the rest of the function (everything outside the body braces) just for the sake of an additional parameter. It might not be a big deal for reset() which is simple and only takes one argument anyway, but for other functions it can be a lot of boilerplate to keep in sync with the main overload: everything from the documentation, templates, parameter names, parameter types, etc. needs to be duplicated and kept in sync.

- Without default arguments, you lose the ability to capture parameters by-value with guaranteed move/copy elision. You have to capture by reference and then construct at least one instance that you otherwise be able to elide. Sure, you don't need that performance all the time, but that's not the point. The point is there are times when you do.

- "Go to definition" in your IDE goes directly to the place you care about; "find all references" finds all references directly.

- Optionals are just way easier to read. Otherwise every reader or maintainer must read every other parameter and ensure they're all forwarded 1:1 without side effects to understand if the semantics of the call are identical with the optional parameter supplied explicitly.

Re: Problems with default function arguments (2020)

#4
As someone who does not do modern C++ development, the problems they bring up seem to largely be of the flavor that positional parameters work poorly with default values in C++. The reason being that all default values do is give you the ability to elide the last N arguments. This is much worse than automatic selection of the correct overload based on the supplied arguments.

However, it seems to me that you could entirely resolve this problem by having true keyword arguments and default arguments must be keyword arguments; they can not be supplied positionally. If you then want some syntactic sugar in your API, you can then do what the author suggests as a solution to default values and supply variants that explicitly pass "defaults" to the master function.

Keyword arguments allows defaults while still allowing exact control over the arguments, exact control over the "overload selected" without explicit variants, and the ability to supply explicit variants if you want API ergonomics. The only downside I can see (relative to the solutions presented by the author) is that you have to write some extra argument names when passing non-default values to any variants that do not trivially pass them through, but that is a pretty minor cost especially with a IDE that can autocomplete the keywords, and it provides extra useful documentation to the maintainer and client, so it is not even all bad.

Re: Problems with default function arguments (2020)

#5
post #4

As someone who does not do modern C++ development, the problems they bring up seem to largely be of the flavor that positional parameters work poorly with default values in C++. The reason being that all default values do is give you the ability to elide the last N arguments. This is much worse than automatic selection of the correct overload based on the supplied arguments. However, it seems to me that you could ent…

Keyword/labeled arguments are also much more robust against API changes breaking silently.

Re: Problems with default function arguments (2020)

#6

All C++ specific though right? Trying to imagine the insanity that would be Numpy (or xyz other Scipy lib) without default function args ...

Yea. And there's even pitfalls the author didn't list. My biggest pet peeve about C++ default arguments it that they are compile time inserted based on the header file. So imagine:

void foo(int a = 1) {}

Now you ship this in a DLL, but realize the default was bad, so you change it to 2 and ship the new DLL. Well, the 1/2 isn't in the DLL at all. It's only in the header file. So everyone must recompile. Fun times.

Re: Problems with default function arguments (2020)

#7
Default arguments are used for sake of the DRY principle. Whenever it is highly likely that the majority of cases of application of a specific function are going to use the same parameter value it can be considered reasonable to imply this parameter value as a default while allowing it to be specified explicitly whenever it make sense to tweak it. Using overloading to implement this pattern would be way more verbose, introduce unnecessary complexity and actually repeat some code.

Re: Problems with default function arguments (2020)

#8
post #4

As someone who does not do modern C++ development, the problems they bring up seem to largely be of the flavor that positional parameters work poorly with default values in C++. The reason being that all default values do is give you the ability to elide the last N arguments. This is much worse than automatic selection of the correct overload based on the supplied arguments. However, it seems to me that you could ent…

This is what C# does, for example.

Re: Problems with default function arguments (2020)

#9
post #4

As someone who does not do modern C++ development, the problems they bring up seem to largely be of the flavor that positional parameters work poorly with default values in C++. The reason being that all default values do is give you the ability to elide the last N arguments. This is much worse than automatic selection of the correct overload based on the supplied arguments. However, it seems to me that you could ent…

In the Julia language overloading only considers positional arguments (which must be given positionally) with keyword arguments entirely separate (and they must be given as keyword arguments).

I quite like that approach.

Re: Problems with default function arguments (2020)

#10
post #4

As someone who does not do modern C++ development, the problems they bring up seem to largely be of the flavor that positional parameters work poorly with default values in C++. The reason being that all default values do is give you the ability to elide the last N arguments. This is much worse than automatic selection of the correct overload based on the supplied arguments. However, it seems to me that you could ent…

This is what C# does, for example.

Not really. You can have default values for positional arguments just fine in C#.
Post reply on HN