Skip to main content
added 1 character in body
Source Link
mtj
  • 4.9k
  • 11
  • 21

Basically, whenever you find yourself doing something like:

MyDataStructure data;

call1(data); // modifies data
call2(data); // modifies data
call3(data); // modifies data

you have a big indicator, that the data structure should indeed be a class:

MyDataClass dataClass;

dataClass.call1();
dataClass.call2();
dataClass.call3();

If the call chain depends on a given order, encapsulate this in the class as well:

MyDataClass {

   public void doComplexTask() {
       call1();
       if (resultIsNotGood) throw ExcepionException...
       
       call2();
       ...
       // you get it
   }

   private call1() { ... } // not exposed any more
}

Basically, whenever you find yourself doing something like:

MyDataStructure data;

call1(data); // modifies data
call2(data); // modifies data
call3(data); // modifies data

you have a big indicator, that the data structure should indeed be a class:

MyDataClass dataClass;

dataClass.call1();
dataClass.call2();
dataClass.call3();

If the call chain depends on a given order, encapsulate this in the class as well:

MyDataClass {

   public void doComplexTask() {
       call1();
       if (resultIsNotGood) throw Excepion...
       
       call2();
       ...
       // you get it
   }

   private call1() { ... } // not exposed any more
}

Basically, whenever you find yourself doing something like:

MyDataStructure data;

call1(data); // modifies data
call2(data); // modifies data
call3(data); // modifies data

you have a big indicator, that the data structure should indeed be a class:

MyDataClass dataClass;

dataClass.call1();
dataClass.call2();
dataClass.call3();

If the call chain depends on a given order, encapsulate this in the class as well:

MyDataClass {

   public void doComplexTask() {
       call1();
       if (resultIsNotGood) throw Exception...
       
       call2();
       ...
       // you get it
   }

   private call1() { ... } // not exposed any more
}
Source Link
mtj
  • 4.9k
  • 11
  • 21

Basically, whenever you find yourself doing something like:

MyDataStructure data;

call1(data); // modifies data
call2(data); // modifies data
call3(data); // modifies data

you have a big indicator, that the data structure should indeed be a class:

MyDataClass dataClass;

dataClass.call1();
dataClass.call2();
dataClass.call3();

If the call chain depends on a given order, encapsulate this in the class as well:

MyDataClass {

   public void doComplexTask() {
       call1();
       if (resultIsNotGood) throw Excepion...
       
       call2();
       ...
       // you get it
   }

   private call1() { ... } // not exposed any more
}