Hi,
I frequently find myself writing checks for things that, in all likeliness, will never happen (and if they did, would be somebody else's fault). While I think this is great in some cases, in others it leads to writing lots of redundant and inefficient code.
For example, in my reflection system you can set and get the values of reflected Properties, like so:
void PropertyInfo::SetValue(Variant owner, ImmutableVariant value)
{
if (owner.GetType().IsCastableTo(*_ownerType) && value.GetType().IsCastableTo(*_propertyType))
{
_setter(owner.GetValue(), value.GetValue());
}
}
As you can see, I'm checking the validity of the arguments before passing them to the setter. The problem with this is that in all likeliness, you wouldn't have gotten this property through anything other than the type information of "owner", in which case the check on "owner" is redundant. This kind of stuff appears all over the place, and not only is it becoming a pain in the ass to write, but it's frequently just reaffirming what I already know.
Anyway, I can see a few solutions to this:
1) Keep the check in the form of an "assert", so that it gets compiled down to nothing in Release mode.
2) Change the arguments to "void*" and "const void*", respectively; indicating that the arguments are not checked, since that is the caller's responsibility.
3) Redesign the way this is used so that the check is never needed (so something like UE4's "TFieldIterator").
I'm really leaning towards option 2 in this case, since Reflection is only going to be used in a few tightly-controlled portions of the codebase; but I think this is question applies elsewhere as well.
What do you think?