This post originated from an RSS feed registered with Java Buzz
by scot mcphee.
Original Post: Funny little code snippet (how not to do it)
Feed Title: SystemExxception()
Feed URL: http://www.autonomous.org/pebble/systemexxception/rss.xml?category=Java
Feed Description: java design and culture by scot mcphee
Here's some code that was discovered this afternoon in a home-grown validation framework. Read it and weep. Tommorrow I just have to review the CVS log to find out the developer responsible. This sort of code is all through certain sections. I kid you not.
public Collection validateAttribute(String modelName, Object value, Collection errors)
{
// ignore nulls
if (value != null)
{
// ensure it is a string
if (!(value instanceof String))
{
try
{
throw (Throwable) new ValidatorException(
getModelName()
+ "."
+ getAttributeName()
+ " - FieldLengthRule validation rule only "
+ "applies to String attributes");
}
catch (Throwable e)
{
e.printStackTrace();
}
}
String str = (String) value;
if (str.length() > getLength())
{
errors.add(new MaxFieldLengthError(modelName, this));
}
}
return errors;
}