I'm a fan of using assertions in the code to check for preconditions. The question is whether is it wise to use assertions in public methods to check the precondition?
My personal opinion is, that if my method can not handle the absence of a parameter (or some invalid value of it) and it is part of an API but does not rely on user input, then the issue (bad parameter) must be handled during the development process (at least by the integration tests) and not later when the software is used on field. For this reason I do use assertions even in public methods.
I mean something like this:
public class SomeMagic implements MagicAPI {
public WoW doSomeMagic( Stuff source ) {
assert source != null : "Incorrect use of the API";
// if assertions are deactivated (should be the case in production environment)
// this line will throw a NpEx
if ( source.isPlainStuff() ) {
// some magic with a plain stuff
}
return Wow.beSurprised;
}
}