-3

I've been reading an agile book about clean coding, mainly in Java and c#. Considering the concept of differentiating between a data class and a logic/object class. I have the following situation in c++, where I can't decide which variant is a clean code. I have a Plane class with some attributes and these attributes change when only one of them changes, so

First Method

class Plane3D {
   public:
      BBox bbox;
      float l,w,h;
      void setbbox(BBox bbox) {
         this->bbox = bbox;
         recalculateLWH();
      }
      void setLWH(float l, float w, float h) {
          //set here
          recalculateBBOX();
      }
};

This makes sense to me, since to the user he is just calling one method and doesn't have to care about internal work of the class. but this is a violation for a data class, that it contains logic Now the

Second method

class Plane3D {
   public:
     BBox bbox;
     float l,w,h;
     void setbbox(BBox bbox) {
             this->bbox = bbox;
          }
     void setLWH(float l, float w, float h) {
      //set here LWH
     }  
};

int main() {
  BBox bbox;//init here
  Plane plane;
  plane.setBBox(bbox);
  recalculateLWH(plane);
}

Now the second method actually separates the data class from the implementation but it increases the responsibilities of the class user and forces him to make an extra call. To my understanding the second method is the correct one from an agile POV, but I find the first method more logical to use.

I'd like to know which of the two methods would make more sense for you guys to undertsand and use

Regards

Moataz Elmasry
  • 2,509
  • 4
  • 27
  • 37

1 Answers1

-1

In this case I think you should prefer first method to second.

  • Your method calls should transform object from one correct state to another correct state. Using the second method after your setBBox(bbox); call your object moves to some incorrect state indeed.

But 'logic class way' can however take place in another situation.

  • Consider you should move plane:) from hangar to landing strip. Now it will be naturally to introduce new class named Tractor and use it like tractor.move(plane)
Lol4t0
  • 12,444
  • 4
  • 29
  • 65