Conditions that should never occur if the code is not correct should be checked with statements. This way you can disable them in production assemblies.
But I do not like to leave a simple assert false; , and I don’t want to write stupid messages either, such as a case of a default in the switch that should never fire, x should be unnecessary, * it should never happen. ", but it is. Whatever it is. Besides doesn’t it sound like a little whining when you see a message complaining that something should not be? Also I would not like to have tons of these messages in the executable file, as they are usually never used, and each of them only relevant for a small function of thousands of functions.
So i like
assert false : "Programming Error"
A programming error is what prevented the application from working so that it is fully consistent with the situation.
switch (x) { case GO_FORWARD: ... break case BUY_SWORD; ... break default: assert false : "Programming Error" } try { buy_sword(); buy_elixir(); } catch (InsufficientFunds) { throw new AssertionError("Programming Error"); }
If you want to constantly perform these checks, instead
assert false : "Programming Error" assert expr : "Programming Error"
do
if (! expr) throw new Exception("Programming Error")
or even get a ProgrammingError exception class.
Adrian panasiuk
source share