At work we have a lot of code like this (pseudocode):
response form_submit(string username, string password) {
if ( username == ""
|| username.contains(invalid_chars)
|| password.length < 5
|| ...
) {
return "errors: blah blah";
} else {
try {
db("insert into users(username, password) values (?, ?)", username, password);
return "success";
}
catch (DBException e) {
return "errors: blah blah";
}
}
}
Some of those are enforced as constraints on the DB, some aren't. It makes more sense to me to enforce everything as DB constraints and just do
response form_submit(string username, string password) {
try {
db("insert into users(username, password) values (?, ?)", username, password);
return "success";
}
catch (DBException e) {
return "errors: blah blah";
}
}
My opinion is obvious here, but what's yours: is this kind of application-level data integrity check bad practise?