i have a model class that represents a table in a database.
The table has 7 columns so the class has 7 attributes. A few of them are allowed to be null, others are not.
So when i pass a person to a public function i check if the not allowed to be null attributes aren’t null. With the normal check like
if(person.getName() == null || person.getSurname() == null ...)
throw new NullPointerException();
I now wonder if there are any downsides of making a function in the Person class that basicly does this? Like
public void checkFullDataset()
{
if(name == null || surname() == null ...)
throw new NullPointerException();
return;
}
so that whenever i work with a person, i call this method instead of manually testing all fields. Would also be of advantage if at the future the table would be changed (new field added, null/not null behaviour changed etc) cause i would only need to change this function instead of many “if’s” all around the code.
Two ideas:
Better to disable the empty constructor and introduce one which takes all fields as argument and put the check there.
If you want to add such method, it better makes session to return
booleansimilar to standardcheckfunction such ashasElement(),hasNext()andiEmpty()etc and let the user deal with it.