r/cpp • • Apr 19 '20

Default function arguments are the devil

https://quuxplusone.github.io/blog/2020/04/18/default-function-arguments-are-the-devil/
90 Upvotes

149 comments sorted by

View all comments

Show parent comment

4

u/Gotebe Apr 19 '20

In your second paragraph, you really give an argument against poor code change and poor code in general, not so much against default parameters.

In overloads situation, the new parameter that alters the behavior means that the code is already bad. It is bad because that one variant being different is not how overloads are used. Surely foo(p1, p2) and foo(p1, p2, p3) cannot possibly mean "explode" and "implode", respectively? (I know, I exaggerate...) Next, even with overloads, if they do the same with variants, chances are, they should be one thing with variations, not a copy-paste.

On the other hand, he who added another default parameter, but made a bug, we'll, they made a bug.

1

u/cat_vs_spider Apr 19 '20 edited Apr 19 '20

I agree with everything you said. But I've spent the last few weeks cleaning up a mess created by somebody else due to a function that has a new default bool that nobody knows about.

Using your example, it's usually something like void destroy(Foo * f, Bar * b, bool shouldExplode = true). Usually, everybody "knows" that destroy explodes and that explosions are the only type of destruction, but now it can implode, and the difference is important to code that actually needs implosions, which is a new use case.

Edit: just to clarify: I agree that foo shouldn't explode and implode. But adding a default argument makes it just so easy to do that.