Java代码优化:if else将PMD复杂度解析为24到0

时间:2015-08-04 06:00:25

标签: java if-statement optimization switch-statement cyclomatic-complexity

我有已经定义了大小的数组字符串,我必须迭代这个数组,然后用someObject的字段值填充它。如果我使用if elseif elseif或switch case这样做我会变得很复杂..请建议正确的方法来做到这一点

enter image description here

1 个答案:

答案 0 :(得分:0)

我建议您使用地图来保持这样的映射:

  private static final Map<Integer, String> MAP_VALUES = new HashMap<Integer, String>();

  private static Map<Integer, String> getMapValues(Employe employe){
    if(MAP_VALUES.isEmpty()){
      MAP_VALUES.put(0, employe.getEmployeeI());
      MAP_VALUES.put(1, employe.getEmployeeLastName());
      MAP_VALUES.put(2, employe.getEmployeeDivision());
      ....
    }
    return MAP_VALUES;
  }

  public String getValueForEmploye(Employe employe){
    String[] value = ...;
    for(int i = 0; i < value.length; i++){
      value[i] = getMapValues(employe).get(i); 
    }
  }