Yes, this is a good practice.
In the case of Spring, this is especially important because validations validate property parameters, etc., which usually come from XML posting files. In other words, they check the webapp configuration. And if you ever do any serious Spring development, these checks will save you hours of debugging when you make a stupid configuration error.
But note that there is a big difference between the Assert library class and the Java Assert keyword, which is used to define a Java statement. The last form of statements can be disabled at application startup time and should NOT be used to verify the validation of arguments that you always want to execute. It's clear that Spring designers believe that it would be a very bad idea to disable the Webapp configuration health check ... and I agree.
UPDATE
In Java 7 (and later), the java.util.Objects class provides the requireNonNull method of checking if the argument is null and throw an exception. You use it as follows:
SomeType t = ... SomeType tChecked = Objects.requireNonNull(t);
or
SomeType tChecked = Objects.requireNonNull(t, "t should be non-null");
However, note that this method raises a NullPointerException , not an IllegalArgumentException .
Stephen C Mar 14 2018-10-14T00: 00Z
source share