How detailed do you have to break the methods?



  • I'll ask for an example of this class and its designer:

    public class MultiDimensionalTop implements Comparable<MultiDimensionalTop> {
    

    private ArrayList<ArrayList<Boolean>> solutionOnTop = new ArrayList<>();
    private ArrayList<ArrayList<Integer>> interestVars = new ArrayList<>();
    private ArrayList<Long> limit = new ArrayList<>();
    private ArrayList<Double> doubleVallist = new ArrayList<>();
    private ArrayList<Integer> indexOfDoubleValList = new ArrayList<>();
    private Task task;

    public MultiDimensionalTop(Task task) {
    this.task = task;
    for (int i = 0; i < task.getLimitCount(); i++) {
    indexOfDoubleValList.add(-1);
    }
    for (int i = 0; i < task.getLimitCount(); i++) {
    doubleVallist.add(0.0);
    }
    for (int i = 0; i < task.getLimitCount(); i++) {
    interestVars.add(new ArrayList<Integer>());
    }
    ArrayList<Boolean> tmpBool;
    for (int i = 0; i < task.getLimitCount(); i++) {
    tmpBool = new ArrayList<>();
    for (int j = 0; j < task.getVarCount(); j++) {
    tmpBool.add(false);
    }
    solutionOnTop.add(tmpBool);
    }
    for (int i = 0; i < task.getLimitCount(); i++) {
    limit.add(0L);
    }
    }

    Should cycles be taken into separate private methods createDefaultXXXVakues()?
    This will help to make every method short and, in addition, every method will carry out its task. Or it can be considered from the point of view that the designer is not long enough, and in some way fulfils the same task of designing the facility.



  • Like this:

    public MultiDimensionalTop(Task task) {
        this.task = task;
        int limitCount = task.getLimitCount()
        for (int i = 0; i < limitCount; i++) {
    
        indexOfDoubleValList.add(-1);
        doubleVallist.add(0.0);
        interestVars.add(new ArrayList&lt;Integer&gt;());
    
        ArrayList&lt;Boolean&gt; tmpBool = new ArrayList&lt;&gt;();
        int varCount = task.getVarCount();
        for (int j = 0; j &lt; varCount; j++) {
            tmpBool.add(false);
        }
        solutionOnTop.add(tmpBool);
    
        limit.add(0L);
    }
    

    }

    You still have a mistake that you announce the variable once outside the cycle, and then within the N cycle, you assign it a newly created facility. Problems two:

    • It doesn't save resources, but it's confusing the code.
    • After the last passage of the cycle, this variable remains available and something bad can be done with it.

    Announce the variables only in the area where they are needed.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2