I came back to Java after some years working mostly Python and C/C++. I often worked using assert
s among other constructions. Today it's difficult to me to mindset me not to use assertions.
$ java -help 2>&1 | grep -2 "\-ea"
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions with specified granularity
My team does not use -ea
under no circumstances, so I'm using constructs like:
assert COND;
if (! COND) {
throw new AssertionError("COND was not true");
}
Where I would just put assert COND;
.
Often like:
assert variable != null;
if (variable == null) {
throw new AssertionError("variable was null");
}
Where I would just put assert variable != null;
.
Do you have any comment on this or on on how can this be improved?
My team does not use -ea under no circumstances
Well, then change that? Shouldn't be difficult/hurt much to add that to a test server.
assert COND; if (! COND) { throw new AssertionError("COND was not true"); }
Those things are not the same. One can be turned off, the other can't.
Always-enforced validation might be used for sanitation of untrusted inputs.
Assertions on the other hand should only be used to guard against programmer errors.