日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

使用Source Monitor检测Java代码的环复杂度

發布時間:2023/12/19 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Source Monitor检测Java代码的环复杂度 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Today I found a useful free software called “SourceMonitor” which can help to calculate and monitor the java code ( and other programming language like C++, C# etc ) complexity.

For the definition and how to calculate cyclomatic complexity itself, please refer to detail in wikipedia.

In order to demonstrate the usage of this software, I use a very simple java class below for example:

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";}} }

It has three different ways to convert an integer into a month name if possible, or else the string “Invalid” is returned.

(1) Create a new project:

Here you could find out all supported programming language:

(2) specify a project name and locate the directory of SourceMonitor project file. For me I choose to store it into the same path of my test java project.

(3) Specify which source files will be scanned by SourceMonitor:

(4) For the left steps in wizard, just use default settings and finish wizard. Click OK button to start scan.

And soon we get the analysis result. Since we are more interested with the detail of each method, we choose “Display Method Metrics” from context menu.

From the result list it is easily known that the third approach of month name retrieval is much better than the first two ones, no matter regarding complexity or statement number.

You could also view the result list via “Chart Method Metrics” from context menu:

Take the complex graph for example: X axis is the complexity value of each method, the Y axis is the total occurrence of each different complex value.

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的使用Source Monitor检测Java代码的环复杂度的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。