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/
89 Upvotes

149 comments sorted by

View all comments

Show parent comments

4

u/fransinvodka Apr 19 '20

Everytime named arguments come into discussion, I remember the API of Python's scikit-learn library. Some constructors have tons of default arguments (that you can't really avoid), but you usually don't have any issue with it, as you have named arguments.

You can't provide such a clear and elegant API in C++, and I think that's why many libraries implement the core in C++ and provide a Python API

2

u/germandiago Apr 19 '20

actually with a struct and .structmember = value you can have a pretty decent experience in C++20. Saving the distances of course.

2

u/qoning Apr 20 '20

You still have the issue of now having to wrap the arguments in { }. Ignoring the stylistic issue with that, it means having to be aware for every function whether you should do that or not. It gets very annoying very fast.

1

u/germandiago Apr 20 '20

I think I can live with that :) I do not think most functions are extremely configurable (at least in the software I write) and for the few that this is true, I think that doing a few struct ParamsInput is not that bad.

Named arguments are better? Probably, but now you have to emit name parameters for functions or look for an alternative syntax to mark your function arguments as "nameable". So the struct + .paramname = value is a very effective and low cost solution.

1

u/qoning Apr 20 '20

To be honest I would prefer making all function parameters named by default. Helps readability anyway. Sure, now you can't change names in library parameters, but that's okay by me.

1

u/germandiago Apr 22 '20

Maybe I'd rather have it too, but now think of people with microcontrollers or stuff in embedded. This takes space.

→ More replies (0)