Say you have two methods:
Number 1:
void AddPerson(Person person)
{
if(person.Name != null && IsValidDate(person.BirthDate)
DB.AddPersonToDatabase(person);
}
Number 2:
void AddPerson(string name, DateTime birthDate)
{
Person p = new Person(name, birthDate);
DB.AddPersonToDatabase(person);
}
Which of the two methods is the best? I know that the first one is more correct OO-wise, but I feel that the second one is more readable, and you do not need to check that the object is valid, as the parameters guarantee this. I just don't like checking objects anywhere I pass them as parameters. Are there other approaches?
EDIT: thanks for all the answers. To clarify, validation in the constructor and the IsValid method is, of course, a good approach, but in my code the actual state of a person often depends on the context and can vary from method to method. This, of course, could be a sign of poor design.
This code is just an example to describe the problem.