I wonder what is the best practice for checking an object. Is there an additional argument against the case of one or two cases? Is there another way?
I'm not looking for any validation library, I just want to do a simple validation.
Case 1
class A { public void doSomething(MyObject o) { try { validate(o); doSomethingUseful(o); } catch (ValidationException e) { Logger.getLogger().warn(e); } } private void validate(MyObject o) throws ValidationException { if (o.getXYZ() == null) throw new ValidationException("Field XYZ cannot be null"); } private void doSomethingUseful(MyObject o) {
Case 2
class A { public void doSomething(MyObject o) { if (validate(o)) { doSomethingUseful(o); } else Logger.getLogger().warn("Object is invalid"); } } private boolean validate(MyObject o) { if (o.getXYZ() == null) return false; return true; } private void doSomethingUseful(MyObject o) {
jk_
source share