3

I have a class whose constructor takes an object containing the parameters for that class (I'm sure there's a pretty name for this, but I don't know it), and I am considering my options for handling validation of the parameters.

Should I do validation in the main class constructor and just let the parameter wrapper class be a dumb container incapable of questioning any values it is given? And if so, is it good practice to throw IllegalArgumentExceptions, even though it is a member of the wrapper class that is "illegal", and not the object itself? Although I guess an object with "illegal" parameters could be viewed as an "illegal object" in it's own right.

The other option seems to be to do some validation in the parameter wrapper class.

I dunno, to me it sounds "cleaner" to just let the main class keep track of what values it accepts, it doesn't sound like the responsibility of the wrapper class? Then again, I could write a new validation class, but that sounds ... unnecessary? Overkill if you will.

sara
  • 3,521
  • 14
  • 34

3 Answers3

2

The answer to this is that it's entirely up to you and depends entirely on your context, who uses which object, etc. But the "parameter object" you're talking about is called a bean, and what you're asking about is bean validation, which might be worth a google search.

If other people (other classes) will be using your "parameter object", then it might make sense to put the validation inside the object itself. But then again, it also depends on where that object is coming from- is it from a properties file? From a GUI? Possibly either one? Then it might make more sense to validate outside of the object so that the user can be notified in some reasonable way.

There is no one correct answer.

If you decide to do the validation inside the object, bean validation allows you to use annotations to enforce the validity of each variable. More info here.

Edit: You might also consider the case when two different objects might use the same "parameter object" but have completely different ideas for what a valid parameter object is. In that case, it would make more sense to do the validation outside of the object. Like I said, it depends entirely on your context.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
1

Does a parameter wrapper object know that it's invalid? The validity of the data contained within is provided by the context in which it's used.

Simple validation could be performed, such as disallowing nulls in the wrapper object, but it seems appropriate that the validation of the parameters be done by the accepting class. More complex validation is also an option, but may limit your ability to re-use the object in other use-cases.

Consider the case where there might be multiple classes that accept these parameter objects, and one of them accepts null for certain values and the other doesn't. If you explicitly disallow null in the wrapper, then you've eliminated that use case in the class that allows or permits it.

Again, it's strictly up to you and how you plan to use the objects. The validation has to be done somewhere, the question is, who do you want to be responsible for it and how maintainable do you want it to be?

Ryan J
  • 8,275
  • 3
  • 25
  • 28
0

Though its more of a subjective one, I think its better to do the validations in the Wrapper class. Whether wrapper OR main/actual, an Object is an Object and it's integrity should be maintained with the creation. So its a good practice to maintain the integrity of the wrapper object before its being passed to main class.

Pandiri
  • 313
  • 1
  • 4