I'm reviewing codes made by previous employees that served the same project as mine. While I'm reviewing their code, I encountered many Domain
class constraints similar to this:
String title
String notes
static mapping = {
.....
title column: 'title'
notes column: 'notes'
.....
}
static constraints = {
.....
title nullable: false, size: 1..50, blank: true
notes nullable: true, size: 0..500, blank: true
.....
}
I get the point that null
values and empty strings are different, hence the nullable
and blank
constraints. But should you really specify 0
as the minimum length of nullable
columns and specify 1
to those non-nullable
columns?
If it is indeed, then what would be the difference to those Domains
that doesn't use similar constraints? Before reading their codes, I've already coded many Domain
classes just using only the nullable
constraint, and they're working just fine.
I think there is redundancy on the constraints.
I would have refactored it to something like this:
static constraints = {
.....
title nullable: false, max:50
notes nullable: true, max:500
.....
}