-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default constructor and default value #273
Comments
Sorry --- I didn't mean to give you a scare! 😅 First, on the risk of changing the value. While we do reserve the right to do that, I have no plans to actually change it, and I think it's overwhelmingly likely that we never will. (At least --- not to some other value. I'll explain below why we probably will change the manner of initialization.) So why add this text? The point is to encourage users to write code that makes their intent clear. Making a "default" quantity, and then using it in some computation, forces the reader to know the construction policy to know what the value is. And it forces the reader to guess at what the value was meant to be: did the author knowingly use the actual policy? Wrongly assume a different policy? Simply forget to initialize the variable? Initializing the variable eliminates these questions. And thanks to (By the way: in the predecessor to this library, from Uber ATG, I did contractually specify that a default initialized quantity had a value of Now, on the merits of the policy. (Let's start with a huge caveat that initialization in C++ is infamously complicated, and I'm likely to make mistakes. That said, here's my best understanding.) There are a couple of different ways that a default constructor is commonly invoked. First, there's value initialization: // Value initialization:
int i{};
QuantityI<Meters> q{}; For value initialization, setting a nonzero value in (That said, looking at this with fresh eyes, I wonder whether we should just change The other common way to invoke a default constructor is default initialization. // Default initialization:
int i;
QuantityI<Meters> q; Here, So, in summary:
|
This won't change any actual values when the rep is a fundamental arithmetic type, but it is more stylistically consistent with the idea of a "default constructor". We also update our contract to officially promise that we do this. This lets us get rid of the scary/threatening language that "we might change this out from under you!", even though we never had any intention to actually do so. We retain the encouragement to use `au::ZERO`, and to avoid using a default-constructed `Quantity` for anything other than future assignment. Fixes #273.
@StefanoD, having considered this further, I've decided to remove the scary language from the documentation. I think that "default constructed Thanks very much for giving me the invaluable perspective of how the documentation reads to an external user! I've learned from this that I need to be more careful about how I communicate my ideas, and I'll try to do that in the future. |
Hi,
I just found a very shocking news in the documentation regarding default-constructor:
This behaviour would differ from the underlying native type like
double
,int
etc. which is contra-intuitive and thus error-prone!We are using the default-constructor spread in our entire code base, with the assumption, that the default value equals to the default value of the native underlying type!
You cannot expect that users read every single line in your documentation. And even if I know this now, my collegues in my company still don't know this and it is hard to communicate this problem to every developer in a big company.
Documentating it, doesn't solve the problem, as I said: You must never expect, that user are reading carefully every single line of your documentation. I experience myself, that many collegues simply don't read my documentation and they expect, that everything works like they expect it intuitively.
The text was updated successfully, but these errors were encountered: