TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Problems with default function arguments (2020)

31 pointsby flipchartover 1 year ago

7 comments

Veservover 1 year ago
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.<p>However, it seems to me that you could entirely resolve this problem by having true keyword arguments and default arguments <i>must</i> 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 &quot;defaults&quot; to the master function.<p>Keyword arguments allows defaults while still allowing exact control over the arguments, exact control over the &quot;overload selected&quot; 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.
评论 #37843219 未加载
评论 #37842586 未加载
评论 #37842029 未加载
评论 #37842299 未加载
dataflowover 1 year ago
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:<p>- 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.<p>- Without default arguments, you lose the ability to capture parameters by-value with guaranteed move&#x2F;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&#x27;t need that performance all the time, but that&#x27;s not the point. The point is there are times when you do.<p>- &quot;Go to definition&quot; in your IDE goes directly to the place you care about; &quot;find all references&quot; finds all references directly.<p>- Optionals are just way easier to read. Otherwise every reader or maintainer must read every other parameter and ensure they&#x27;re all forwarded 1:1 without side effects to understand if the semantics of the call are identical with the optional parameter supplied explicitly.
评论 #37843553 未加载
theteapotover 1 year ago
All C++ specific though right? Trying to imagine the insanity that would be Numpy (or xyz other Scipy lib) without default function args ...
评论 #37842048 未加载
mattstirover 1 year ago
This may just be my inexperience with the intricacies of C++, but it seems like a decent number of the problems raised here could be fixed by allowing one to name default args like proper keyword arguments. Consider the first issue discussed and how naming the argument just fixes it:<p><pre><code> print_square(&#x27;*&#x27;); print_square(fill=&#x27;*&#x27;); &#x2F;&#x2F; Hypothetical fix </code></pre> It also seems to be an issue exacerbated by C++ implicitly converting a char into an int.<p>&gt; The client programmer doesn’t want a “puzzling” print_square(x) that treats x sometimes as a side length and sometimes as a fill character!<p>This is also fixed if C++ allowed you to name keyword arguments explicitly (and didn&#x27;t sometimes implicitly convert types, but that&#x27;s baked in pretty hard by now).<p>&gt; The “boolean parameter tarpit”<p>Again, imagine how much more understandable it would be if you could write:<p><pre><code> doTask(task1, 100s, catchStderr=true); </code></pre> I wonder whether the author would change their stance on default args if they were made to be more usable.
qwerty456127over 1 year ago
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.
评论 #37844784 未加载
评论 #37843395 未加载
Waterluvianover 1 year ago
Are there any languages that do not have positional arguments at all and they’re always keyword arguments?<p>My pet peeve is reading code and trying to reason about<p><pre><code> foo(true, 1, 1, null, “cupcakes”)</code></pre>
评论 #37843700 未加载
评论 #37844520 未加载
评论 #37847320 未加载
gpderettaover 1 year ago
void log(const std::string_view message, const std::source_location location = std::source_location::current());