Domanda

The following java code gives a rather high Cyclomatic Complexity. I would like to find a way to properly reduce it. How would I best do this?

To be clear, the code gets the corresponding result of a value, based on whether the value is in between two limits. The value itself could be any integer between -10000 to +200000 The problem lies mostly with the "Less Than or Equal" operators, which prevent the simple use of a library. The range between two limits can be different and we're talking ranges in the 10000s, an example interval would be [<0...10000....25000...32500...]. The ranges are rather arbitrary numbers, decided upon by the business.

You can assume that the LIMIT values are constants, defined at the beginning of the class. The same goes for the set result values. Changing them from a constant to something else is possible.

Any ideas?

private int function getBasedOnInterval(int value){
  int result;
  if(value <= 0){
    result = RESULT1;
  }else if(value <= LIMIT1){
    result = RESULT2;
  }else if(value <= LIMIT2){
    result = RESULT3;
  }else if(value <= LIMIT3){
    result = RESULT4;
  }else if(value <= LIMIT4){
    result = RESULT5;
  }else if(value <= LIMIT5){
    result = RESULT6;
  }else if(value <= LIMIT6){
    result = RESULT7;
  }else if(value <= LIMIT7){
    result = RESULT8;
  }else if(value <= LIMIT8){
    result = RESULT9;
  }else if(value <= LIMIT9){
    result = RESULT10;
  }else if(value <= LIMIT10){
    result = RESULT11;
  }else if(value <= LIMIT11){
    result = RESULT12;
  }else if(value <= LIMIT12){
    result = RESULT13;
  }else if(value <= LIMIT13){
    result = RESULT14;
  }else{
    result = RESULT15;
  }
  return result;
}
È stato utile?

Soluzione

One first step for refactoring this could be to put all you limits into an array or list and then iterate over it and test every limit:

private int function getBasedOnInterval(int value) {
    int result = RESULT15;

    // consider LIMITS as an array containing 0, LIMIT1...LIMIT13
    // consider RESULTS as an array containing RESULT1...RESULT14
    for(int index = 0; index < LIMITS.length; index++) {
        if(value <= LIMITS[index]) {
            result = RESULTS[index];
            breaks;
        }
    }

    return result;
}

Altri suggerimenti

You are probably looking for BST (Binary Search Tree).

From Wikipedia; Time complexity in big O notation:

       | Average  | Worst case
----------------------------------
Space  | O(n)     | O(n)
Search | O(log n) | O(n)
Insert | O(log n) | O(n)
Delete | O(log n) | O(n)

This will allow you to speed up the search if you create the BST once at the start and simply reuse it. If you were to give us more information regarding the spread of data it may be able to be improved using other techniques

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top