使用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));
else
System.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";
else
return "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的原创文章,请关注公众号"汪子熙":
相关文章
- java bufferedwriter 编码,Java BufferedWriter对象与utf-8
- java笔试题_一道简单的 Java 笔试题,但值得很多人反思
- java怎么输入字符串和一行字符串
- xml格式化 java_Java XML格式化程序
- java webservice 实例_Java WebService 简单实例(附实例代码)
- 【说站】java软引用在队列的应用
- java:commons-configuration2与spring的集成
- JAVA实验室设备管理系统代码_java做一个简单学生管理系统
- java 异或加密_Java异或技操作给任意的文件加密原理及使用详解
- Java 近期新闻:Classfile API 草案、Spring Boot、GlassFish、Reactor 项目
- java通用URL接口地址调用方式GET和POST方式详解架构师
- 中的应用Java中MySQL的灵活运用(mysql在java代码)
- 国内首本系统讲解Java异步编程的书籍-java异步编程实战
- 使用Java连接MySQL数据库的具体操作方法(java连接mysql代码)
- Java程序在Linux系统中实现命令操作(java运行linux命令)
- 增强Java能力学习J2SE和Oracle(j2se oracle)
- 实现基于Redis的分布式锁Java实现(redis锁java代码)
- 本使用Oracle Java 进行升级新版本带来新体验(oracle java版)
- 缓存使用Redis让Java代码更加迅速缓存设置(redis设置java)
- Java线程优先级示例代码