vendredi 27 septembre 2019

Is there any structure other than if-else to do condition specific action?

Assume I have a class (classBase) interacting with several classes (class1, class2, class3, ...). This class (classBase) does some action based on target variables in other classes. In the code below, a classNComplete can only be true if classN.variable is greater than one (please see the code below).

(Assume that it can enter to the while loop in the first run)

Code in classBase

while (!(class1Complete && class2Complete && ...)) {
  if (class1.variable > 1) {
    /* do something; class1Complete = true; */ } else {
    /* do something; class1Complete = false; */ }

  if (class2.variable > 1) {
    /* do something; class2Complete = true; */ } else {
    /* do something; class2Complete = false; */ }
  ...
}

Since there are lots of classes to check if the variable is greater than 1, I find this code very cumbersome. I am wondering if there is another implementation to this kind of problems.

Aucun commentaire:

Enregistrer un commentaire