Let's say I have a class like:
class NavigationData
{
float roll;
float pitch;
double latitude;
double longitude;
}
and if I want to create a method:
const bool validate() const;
which basically validates whether the 4 fields contain valid values.
Should validate() be part of NavigationData class, or should I create something like a NavigationDataValidator, that contains a validate(const NavigationData&) method.
I'm just giving a simple example, obviously my real class is a lot more complicated than this. I'm looking for good OO principles.
Put it another way: given a method, how do we know if it should belong to the class, or should belong to a separate class?
The right answer is: it depends.
The natural place to put such object logic is in the object itself. Sometimes though the validation rules could be dependent on a rules engine or some kind of larger "framework" which validates stuff. Another situation where you wouldn't want to do validation inside the object is when validation belongs in another tier, or layer or the application, such as the view layer.
Depends...
Sometimes the validations are not part of the class itself, but business logic, and adding it to the class would prevent reutilization, and therefore, the usage of a validating class is good. Otherwise, the class should be able to validate itself.
It depends on the context. Sometimes, your object is absolutely valid internally, but in the context its values aren't acceptable.
If you have simple Data Transfer Object then it probably shouldn't validate itself.
If you have a class of Domain Model then it should do some validation.
P.S. just my personal preference: isValid () instead of validate () when method returns boolean value.
As it has already been said, it depends.
But in your case, I would go for a different solution, create new immutable class for geo coordinates
And let this class validate that latitude and longitude are within valid limits. You will probably need this other places as well, for example.
Typically it is a class's own responsibility to ensure that it maintains a logically consistent and valid internal state. For instance,
Person
may have a constructor that requires both a first and last name if operations onPerson
are meaningless without this data.However, "logically consistent and valid" is different from "makes sense in the domain", so it is sometimes the responsibility of an external class to ensure that domain rules are obeyed. For example,
PersonValidator
may require thatPerson
has a phone number which is in the US. ButPerson
shouldn't necessarily need to know anything about whether or not aPhoneNumber
is in the US.A good rule of thumb is that if state or domain rules external to the class are required in addition to the data that already belongs to the class, you will want to consider having external validation. You may also want to centralize validation in an external class if the state of the class's instances can come from multiple sources (e.g., database, web form, file, etc.).
Your class should be designed such a way and provide such methods that validate() is always true:
Such validate() methods are called class invariants and are important part of Design by Contract.