使用工具Source Monitor測量您Java程式碼的環複雜度

i042416發表於2018-08-09

程式碼的環複雜度(Cyclomatic complexity,有時也翻譯成圈複雜度)是一種程式碼複雜度的衡量標準,在1976年由Thomas J. McCabe, Sr. 提出。

來看看計算公式。

程式碼環複雜度 = E − N + 2

E = 程式控制流圖中邊的個數

N = 程式控制流圖中點的個數

很容易得出這樣的結論:程式碼環複雜度越高,越容易出bug。

使用工具Source Monitor測量您Java程式碼的環複雜度

可以想象如果需要開發人員自己去把一段程式碼的控制流圖畫出來,然後去數圖中邊和點的個數,這種做法效率太低了也容易出錯。

好訊息是,有一款名為Source Monitor的免費軟體,能夠幫我們來度量Java程式碼的環複雜度。當然這款軟體也支援C++和C#。

使用工具Source Monitor測量您Java程式碼的環複雜度

為了說明如何使用這款軟體,我寫了一段簡單的Java程式碼。

package test;import java.util.ArrayList;public class monthTool {static ArrayList<String> monthCollection = new ArrayList<String>();public static void main(String[] args) {
monthTool tool = new monthTool();
tool.printV1(1);
tool.printV2(2);
tool.printV1(0);
tool.printV2(-1);
tool.printV3(3);
tool.printV3(13);
}public monthTool(){
monthCollection.add("Invalid");
monthCollection.add("January");
monthCollection.add("Febrary");
monthCollection.add("March");
monthCollection.add("April");
monthCollection.add("May");
monthCollection.add("June");
monthCollection.add("July");
monthCollection.add("August");
monthCollection.add("September");
monthCollection.add("October");
monthCollection.add("November");
monthCollection.add("December");
}public void printV1(int month){
System.out.println("Month is: " + getMonthNameV1(month));
}public void printV2(int month){if( month >= 1 && month <= 12)
System.out.println("Month is: " + getMonthNameV2(month));elseSystem.out.println("Please specify a valid month");
}public void printV3(int month) {
System.out.println("Month is: " + getMonthNameV3(month));
}public String getMonthNameV2(int month){if( month == 1)return "January";else if( month == 2)return "Febrary";else if( month == 3)return "March";else if( month == 4)return "April";else if( month == 5)return "May";else if( month == 6)return "June";else if( month == 7)return "July";else if( month == 8)return "August";else if( month == 9)return "September";else if( month == 10)return "October";else if( month == 11)return "November";else if( month == 12)return "December";elsereturn "Invalid";
}public String getMonthNameV1(int month){switch (month){case 1:return "January";case 2:return "Febrary";case 3:return "March";case 4:return "April";case 5:return "May";case 6:return "June";case 7:return "July";case 8:return "August";case 9:return "September";case 10:return "October";case 11:return "November";case 12:return "December";default:return "Invalid";
}
}public String getMonthNameV3(int month){try {return monthCollection.get(month);
}catch (java.lang.IndexOutOfBoundsException e){return "Invalid";
}
}
}

其中我用了三種不同的方式實現了同一個邏輯,將一個代表月份的整數轉成了月份名稱。

下面是Source Monitor的具體用法。

1. 建立一個新的專案:

使用工具Source Monitor測量您Java程式碼的環複雜度

這裡能看到所有Source Monitor支援的程式語言。

使用工具Source Monitor測量您Java程式碼的環複雜度

2. 指定您本地的Java專案檔案地址:

使用工具Source Monitor測量您Java程式碼的環複雜度

3. 指定您的Java專案資料夾內,您希望SourceMonitor計算哪些Java檔案的環複雜度。

使用工具Source Monitor測量您Java程式碼的環複雜度

4. 點OK,就可以開始掃描啦。

使用工具Source Monitor測量您Java程式碼的環複雜度

很快Source Monitor就將我們指定的Java檔案的環複雜度計算完畢。點選選單“Display Method Metrics”來檢視結果:

使用工具Source Monitor測量您Java程式碼的環複雜度

從環複雜度掃描結果能看出,明顯第三種從月份名稱集合裡通過ArrayList自帶的get方法取得月份名稱是最優的解法——環複雜度僅為2。

使用工具Source Monitor測量您Java程式碼的環複雜度

也可以通過圖表的方式更直觀得看到方法的環複雜度比較:

使用工具Source Monitor測量您Java程式碼的環複雜度

X軸的值代表每個方法的環複雜度,Y軸代表這些環複雜度的不同值出現的次數。

比如下圖的意思是,環複雜度為1的方法(X軸刻度為1的節點)共有4個(Y軸刻度為4),環複雜度為2的方法(X軸刻度為2的節點)有1個(Y軸刻度為1)。以此類推。

使用工具Source Monitor測量您Java程式碼的環複雜度

要獲取更多Jerry的原創技術文章,請關注公眾號"汪子熙"或者掃描下面二維碼:


使用工具Source Monitor測量您Java程式碼的環複雜度

使用工具Source Monitor測量您Java程式碼的環複雜度


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2199506/,如需轉載,請註明出處,否則將追究法律責任。

相關文章