Tuesday, July 14, 2009

Lessons from the validators

In my previous Java project, I had to code validators for a bunch of classes that have no concrete requirements specified and I was a bit clueless as to how big is the implementation scope and how should I validate the text fields that may accept different kinds of patterns of different data types. First of all, they needed a field that could accept only * and 0-9, and naturally Regex came to mind........ and that being a former PHP scripter, I inadvertently used regex in all of my validations, which, in the end, was not as elegant as I thought! (Although some seasoned OOP developers may feel regex is unnecessary, in my opinion it is quite handy.)

As I struggled in getting the correct method to use and at the same time getting help from the seniors, I realised that I could have separated my validation methods into regex parsing and the regular numeric data type parsing. And then, I have decided that the middle road is the way to go in my future projects.

This is my conclusion :
1) If the field consists of special characters and/or alphanumeric -> use Java regex
2) If the field is only tied to one data type, for instance only numeric -> use the Java number class to do the parsing e.g. Double.parseDouble

Mix and match is good eh?

No comments:

Post a Comment