• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding validation to a constructor  RSS feed

 
Vance Rodriguez
Greenhorn
Posts: 5
Eclipse IDE Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to add validation to some of the elements in my constructor.


I want to be able to check that the data for the ID is limited to a certain collection of characters formatted in a certain. For example, I may wish to limit it to 5 lowercase letters or numbers, or a combination of both. How could I do this?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vance Rodriguez wrote:I want to be able to check that the data for the ID is limited to a certain collection of characters formatted in a certain. For example, I may wish to limit it to 5 lowercase letters or numbers, or a combination of both. How could I do this?

Well, basically the same as you might do anywhere else: write code to validate it.

I suspect that what you probably want is some form of regular expression, but I'm not exactly sure. And those are encapsulated in the Pattern (java.util.regex.Pattern) and Matcher (java.util.regex.Matcher) classes, and also in the String.matches() method.

The only thing I'd say is that the two things - construction and validation - are completely separate, so keep the logic for them in different places. And the simplest way to do that is to have the constructor call a validation method - which could even be in a separate class.

HIH

Winston
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is purely an aesthetic choice, but I do my validation at the input stage. When accepting user input data, I do all the verification before allowing the program to progress (i.e. the user is stuck on the input screen until the data is what it is supposed to be).

Regards,
Robert
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote:This is purely an aesthetic choice, but I do my validation at the input stage. When accepting user input data, I do all the verification before allowing the program to progress (i.e. the user is stuck on the input screen until the data is what it is supposed to be).

Hmmm. Not sure I agree with that.

If an object must be correct, then the logical place to do the validation is in the constructor, because that ensures that an incorrect object can NEVER be created.

What you're talking about is ensuring that those checks are redundant, and I have absolutely no problem with that; but I'd never trust a procedure to make sure that an object of mine is correct. If it's wrong, it's wrong, and the only way to make sure that it's correct in all cases is to put those checks in the constructor.

My 2¢.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:If an object must be correct, then the logical place to do the validation is in the constructor, because that ensures that an incorrect object can NEVER be created.

@Robert: Quick example for you: I've been working on a Fraction class for some time now (a class that hold two numbers: a numerator and divisor), and one variant of it is a RealFraction - ie, a Fraction that holds a "real number".

After working through it for some time, I decided that the easiest way to ensure it was to make the class prevent itself from being instantiated with a divisor of 0, because there were just too many possibilities for why it might happen. And that one decision allowed me to remove a pile (and I do mean a pile) of "preventive" code and replace it with a simple ArithmeticException.

Hope it explains my reasoning.

Winston
 
Vance Rodriguez
Greenhorn
Posts: 5
Eclipse IDE Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Vance Rodriguez wrote:I want to be able to check that the data for the ID is limited to a certain collection of characters formatted in a certain. For example, I may wish to limit it to 5 lowercase letters or numbers, or a combination of both. How could I do this?

Well, basically the same as you might do anywhere else: write code to validate it.

I suspect that what you probably want is some form of regular expression, but I'm not exactly sure. And those are encapsulated in the Pattern (java.util.regex.Pattern) and Matcher (java.util.regex.Matcher) classes, and also in the String.matches() method.

The only thing I'd say is that the two things - construction and validation - are completely separate, so keep the logic for them in different places. And the simplest way to do that is to have the constructor call a validation method - which could even be in a separate class.

HIH

Winston

Thanks! A regular expression is exactly what I need. Any useful resources on how to code these?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vance Rodriguez wrote:Thanks! A regular expression is exactly what I need. Any useful resources on how to code these?

Well, i've found this site to be pretty good. But be warned: Regular expressions look simple - and for many basic pattern operations they're wonderful - but as soon as you start "wildcarding" or "backtracking" they can quickly become very arcane indeed, AND easy to get wrong.

So my advice: Keep it simple.

It sounds like your pattern should be pretty straightforward, but if you run into problems check the tutorial on that site (and there are others), or come back here.

Winston
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vance Rodriguez wrote:Thanks! A regular expression is exactly what I need.

Jamie Zawinski, in a Tue 12 Aug 1997 Usenet post wrote: Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.

Some ideas stand the test of time... Jamie's quip was meant to be humorous I'm sure but there's also a lot of good advice in there you might want to think about. Winston echos the advice:

Winston Gutkowski wrote:But be warned: Regular expressions look simple - and for many basic pattern operations they're wonderful - but... they can quickly become very arcane indeed, AND easy to get wrong.

So my advice: Keep it simple.

I also agree with Winston's advice to keep instantiation and validation separate. In this case, I would probably try a private member class.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Robert D. Smith wrote:This is purely an aesthetic choice, but I do my validation at the input stage. When accepting user input data, I do all the verification before allowing the program to progress (i.e. the user is stuck on the input screen until the data is what it is supposed to be).

Hmmm. Not sure I agree with that.

If an object must be correct, then the logical place to do the validation is in the constructor, because that ensures that an incorrect object can NEVER be created.

What you're talking about is ensuring that those checks are redundant, and I have absolutely no problem with that; but I'd never trust a procedure to make sure that an object of mine is correct. If it's wrong, it's wrong, and the only way to make sure that it's correct in all cases is to put those checks in the constructor.

My 2¢.

Winston

I stand corrected. In my defense, I was not thinking OO, but procedural. You are correct that I cannot depend on a caller to pass a correct object.

Regards,
Robert
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!