Should a class validate itself or create another class to validate it?

后端 未结 8 782
梦毁少年i
梦毁少年i 2021-02-01 17:30

Let\'s say I have a class like:

class NavigationData
{
  float roll;
  float pitch;
  double latitude;
  double longitude;
}

and if I want to c

8条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 18:08

    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.

提交回复
热议问题