0

The Cyclomatic Complexity of this method "mapRow" is 13 which is greater than 10 authorized从下面的代码中得到圈复杂度():

public RedemptionReport mapRow(ResultSet rs, int row) throws SQLException {        
        RedemptionReport redemptionReport = new RedemptionReport();
        redemptionReport.setRedeemDate(rs.getString(1));
        redemptionReport.setCashierID(rs.getString(2) != null? rs.getString(2) : "");
        redemptionReport.setTillNo(rs.getString(3) != null? rs.getString(3) : "");
        redemptionReport.setReferenceNumber(rs.getString(4) != null? rs.getString(4) : "");
        redemptionReport.setTransactionNumber(rs.getString(5) != null? rs.getString(5) : "");
        redemptionReport.setRedemptionAmount(rs.getString(6) != null? rs.getString(6) : "0");
        redemptionReport.setNetBillValues(rs.getString(7) != null? rs.getString(7) : "0");
        redemptionReport.setStoreCode(rs.getString(8) != null? rs.getString(8) : "");
        redemptionReport.setCardNumber(rs.getString(9) != null? rs.getString(9) : "");
        redemptionReport.setCardType(rs.getString(10) != null? rs.getString(10) : "");
        redemptionReport.setStoreDesc(rs.getString(11) != null? rs.getString(11) : "");
        redemptionReport.setZoneDesc(rs.getString(12) != null? rs.getString(12) : "");
        redemptionReport.setMobileNo(rs.getString(13) != null? rs.getString(13) : "");
        redemptionReport.setSchemeName(rs.getString(14));
        return redemptionReport;
    }

如何从上述代码中消除这种复杂性?

4

3 回答 3

2

创建一个封装三元运算符的方法,例如:

private String get(String val, String def) {
    return val != null ? val : def
}

或者更简单:

private String get(String val) {
    return val != null ? val : ""
}
于 2016-06-20T05:10:09.163 回答
1

覆盖 RedemptionReport 的设置器。这应该属于域类,因为默认值仅在 RedemptionReport 类中才有意义。将“rs.getString(x)”值传递给 setter 方法。

于 2016-06-20T05:17:30.970 回答
1
public RedemptionReport mapRow(ResultSet rs) throws SQLException {        
        RedemptionReport redemptionReport = new RedemptionReport();
        redemptionReport.setRedeemDate(this.getResultFromResultSet(rs.getString(1)));
        redemptionReport.setCashierID(this.getResultFromResultSet(rs.getString(2)));
        redemptionReport.setTillNo(this.getResultFromResultSet(rs.getString(3)));
        redemptionReport.setReferenceNumber(this.getResultFromResultSet(rs.getString(4)));
        redemptionReport.setTransactionNumber(this.getResultFromResultSet(rs.getString(5)));
        redemptionReport.setRedemptionAmount(this.getResultFromResultSet(rs.getString(6)));
        redemptionReport.setNetBillValues(this.getResultFromResultSet(rs.getString(7)));
        redemptionReport.setStoreCode(this.getResultFromResultSet(rs.getString(8)));
        redemptionReport.setCardNumber(this.getResultFromResultSet(rs.getString(9)));
        redemptionReport.setCardType(this.getResultFromResultSet(rs.getString(10)));
        redemptionReport.setStoreDesc(this.getResultFromResultSet(rs.getString(11)));
        redemptionReport.setZoneDesc(this.getResultFromResultSet(rs.getString(12)));
        redemptionReport.setMobileNo(this.getResultFromResultSet(rs.getString(13)));
        redemptionReport.setSchemeName(this.getResultFromResultSet(rs.getString(14)));

return redemptionReport;
    }

定义另一个私人消息来做这样的工作(空检查并返回默认值)。

private String getResultFromResultSet(String val){
        return val != null ? val : "";
}
于 2016-06-20T05:25:00.187 回答