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

后端 未结 8 798
梦毁少年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条回答
  •  时光取名叫无心
    2021-02-01 18:10

    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

    class GeoCoordinates
    {
        double Latitude;
        double Longitude;
    }
    

    And let this class validate that latitude and longitude are within valid limits. You will probably need this other places as well, for example.

    class Airport
    {
        GeoCoordinates Location;
        ...
    }
    

提交回复
热议问题