Do you mean a check on "whatever"? Since "whatever" was supposed to be the problem variable, as it was supposed to represent a "new feature" that had been added, and all preexisting code (i.e. before the feature addition) only knew of bla. The code already initializes bla.
I suppose I wasn't understanding the example very well. If "whatever" is a member added to the struct later then what would it being set to null matter for old code? Unless the "old code" tried to utilize it then it would essentially be the same as the transparent addition of adding a new variable to a class your code was using, but not utilizing that particular variable.
I'm assuming the point you're getting at is, someone adds the new "whatever" variable to an object, and your code attempts to use the "whatever" object, but your coworker managing the usage of the struct that passes it to you hasn't implemented setting that whatever to something yet. In that case of course you would have to check validity of the object before you use it. If I'm getting this wrong please do re-explain it to me.
But where would this check go? If "in the code initializing the FooContext", then that means the programmer has to add it to each such piece of code, which creates a lot of duplicated checks
It does, but in the case of a structure it's about the same as if you require a constructor to set the .bla and the .whatever and that whatever may have been changed to a null state after construction anyway.
What it comes down to is that you have two tools to an object really. A reference, which is a guarentee that the object is at least existant even if it may be in an invalid state for your usage, or a pointer, which can be null. I think as I read more into what you're saying, what you're getting at is that by using a constructor you are forcing the caller to pass a constructed object to your structure at the time of creation of the structure, and sure, that's true. But if the structure is designed to allow that information to change you'd have to use a pointer anyway, which would indicate you have to put the "redundant" error checking anyway. So again, it's a tradeoff really. You can use the construction power of the reference but you're only really avoiding a nullptr check, and you're saying explicit to the caller "you can't change this after creation."
And furthermore, the programmer adding such checks is then aware of the existence of the rest of the code, whereas kunos' scenario was that someone adds a new feature ("whatever" in this case) but isn't aware of/forgets to add the corresponding initialization to every place the FooContext is initialized. So if they are to add the checks to wherever the FooContext is initialized, then they know about those places, and so they should be adding proper initialization too, I'd think. But his scenario is that they are not so aware. It seems he wants it "robust against feature addition" or something like that. If the check goes in the constructor, then what about the concern about constructors being bloated with checks?
I'd agree in the case of adding new required dependencies to an object the system of requiring a reference to be passed is more safe, I can't debate that. But like I said you're not actually arguing against having a default constructor in that case you're simply arguing that it is more clear that an object with a constructor requiring a reference is more "informative" and "complicance requiring" than an object that can be changed after construction. Even if you only had one constructor, if you gave a method to change that member at all then you would inherently have to do the same nullptr check as providing a default constructor would.
Thus at the heart of it, the only real debate here is one of style: do you prefer each object to be different and ask for what it deems to be "required" dependencies at construction, or do you make setting the information more transparent and allow a default constructor as well as the overloaded ones of the same style. It's arguably not a very big difference but it has a big effect on how the code is portrayed to others.
Like the SFML example I was using, it isn't as big a deal as you're making it out to be, in the example of the sprite object it really isn't that unintuitive to "know" that you have to set a texture to the sprite before using it, even if you construct it without passing one. However since quite a few people seem more interested in removing "dangerous" code like a default constructor I may experiment more with different mixtures of both methods in some future projects to see how it works out in real world example. My biggest problem with debates using simple class like a foo or a baz or a whatever is that they don't take things like multiple dependencies and subsystems into account or the mutable nature of a lot of game objects. Most game objects are going to have a lot of state you can set after creation, perhaps even major systems, so in that the "reference only" constructor quickly becomes an impossibility.
Edited by Satharis, 05 September 2013 - 04:17 PM.