2 ms·
I don't think you'd want to. One of the primary usages of this kind of thing would be like when you need to pass in some massive struct of options to some dumb
by OskarS 6y ago
I don't think you'd want to. One of the primary usages of this kind of thing would be like when you need to pass in some massive struct of options to some dumb API function (you know the ones!), and then you'd absolutely want default values for all the things you don't want to specify.
Also, some might argue that what you're describing (call sites not becoming invalid when you update the function) is a feature, not a bug. This way, you can add options to your functions and still have everything compile like it should.
I wouldn't personally argue that though, i think it leads to bad practice where functions take a bazillion options and they become horrible tech debt. I'm working in a mixed C++ and Lua codebase, and the Lua codebase is littered with functions that take, like, 9 different arguments that change the behavior of the function. When I asked one of my colleagues about this style, he said it was one of his favorite features of Lua, that you can add arguments to a function to change the behavior in certain cases but keep the original functionality (guarded with default values or if's or whatever). It's made much of the code a living nightmare!
So, I guess, in the process of writing this comment I've come over to your side :) say no to adding arguments with default values!
- cma 6y agoI don't mind it for thing like pyplot where you have tons of style options hidden away like that. But I'd definitely like to be able to say this one isn't optional or implicitly default constructed (or worse uninitialized, maybe that is already precluded by any amount of brace initialization, or maybe not if the class is POD, I'll have to check). Someone suggested below deleting default constructors, you could make a template class that did that and forwarded other constructors, but I'd still like to be able to allow explicit default initialization in the ones that aren't considered optional.