enums
not changing, or if they do, the code must be updated. If not handled properly, this can become a maintenance nightmare.Use of the
assert
keyword comes in handy here. You can assert
in debug builds that the value is what you expect it to be and whoever is maintaining your code will know what they need to do to make their new value work (because, let's face it, you comment your code great).There's one little problem though. The
assert
line only happens if it's executed, therefore it might not catch all situations of misuse (at least not right away).Here's a neat little trick:
#define compiletime_assert(argument) \This gets evaluated at compile time. If anyone changes
{ int unused[ argument ? 1 : -1 ]; }
compiletime_assert(MY_SPECIAL_VALUE == 4);
MY_SPECIAL_VAL
to a value other than 4, the compiler will stop there with an error. The only downside is that the error is pretty cryptic, but good commenting will help out there. Obviously this will only work for things the compiler is able to evaluate at compile time, but that includes some very useful things like sizeof
and value checking.
No comments:
Post a Comment