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代码的环复杂度相关推荐

  1. 使用工具Source Monitor测量您Java代码的环复杂度

    代码的环复杂度(Cyclomatic complexity,有时也翻译成圈复杂度)是一种代码复杂度的衡量标准,在1976年由Thomas J. McCabe, Sr. 提出. 来看看计算公式. 代码环 ...

  2. 如何计算并测量ABAP及Java代码的环复杂度Cyclomatic complexity

    代码的环复杂度(Cyclomatic complexity,有的地方又翻译成圈复杂度)是一种代码复杂度的衡量标准,在1976年由Thomas J. McCabe, Sr. 提出. 在软件测试的概念里, ...

  3. SonarQube 9.x集成阿里p3c代码规范检测java代码;

    文章目录 前言 一.下载p3c-pmd插件 二.sonarqube配置使用p3c规则检测 1.新建质量配置 2.将创建好的p3c检测规则设置为默认质量配置 注1 注2 前言 因为我们公司后端主用的是j ...

  4. 阿里巴巴代码规约检测Java 代码规约扫描 | 云效

    阿里巴巴代码规约检测&云效Java 代码规约扫描<阿里巴巴 Java 开发手册>是阿里巴巴集团技术团队的集体智慧结晶和经验总结,经历了多次大规模一线实战的检验及不断的完善,系统化地 ...

  5. java代码质量 圈复杂度,代码圈复杂度(Cyclomatic Complexity,CC)和Oclint工具 介绍...

    什么是代码圈复杂度 圈复杂度是一种度量程序复杂度的方法,由 Thomas McCabe 于 1976年定义,用来衡量一个模块判定结构的复杂程度,数量上表现为独立路径条数,即合理的预防错误所需测试的最少 ...

  6. 如何以及为什么使用Spoon分析,生成和转换Java代码

    Spoon是分析,生成和转换Java代码的工具. 在本文中,我们将看到通过使用以编程方式处理代码的技术可以实现什么. 我认为这些技术不是很广为人知或使用,这很遗憾,因为它们可能非常有用. 谁知道,即使 ...

  7. windows下 Source Monitor代码度量工具的使用

    windows下 Source Monitor代码度量工具的使用 引用链接: https://www.cnblogs.com/xuehanyu/p/4520965.html 1.总体介绍 Source ...

  8. python代码检测链表中的环并删除环

    python代码检测链表中的环并删除环 在计算机科学中,链表是数据元素的线性集合,其顺序不是由它们在内存中的物理位置决定的.相反,每个元素指向下一个元素.它是一种数据结构,由一组节点组成,这些节点共同 ...

  9. java web源代码_检测Java Web应用程序而无需修改其源代码

    java web源代码 与其他系统进行交互时,大多数Java Web应用程序都使用标准Java接口. 诸如Web页面或REST服务器之类的基于HTTP的服务是使用接口javax.servlet.Ser ...

最新文章

  1. linux中的for命令
  2. Fedora 30系统的升级方法
  3. 使用访问控制列表预防IP地址欺骗(IP Address Spoofing Prevention with ACLs)
  4. 罗辑思维首席架构师:Go微服务改造实践
  5. linux下获取线程号
  6. python io多路复用_python实现IO多路复用 --- selector
  7. 【windows】安装pytorch1.7.1-cpu版本的geometric过程记录
  8. 阿里最后一面,高并发下如何设计一个秒杀系统?
  9. 第十一篇:稳定性之面向失败设计【过载保护】
  10. pyechart图像示例与细节修改资料
  11. BZOJ.3648.寝室管理(点分治 树状数组)
  12. [汇编语言计算机原理] 带开机音乐,速度、进度和行驶方向显示的出租出计费系统设计
  13. openpyxl 简单使用
  14. padavan固件获取网络地图中的客户端状态
  15. while循环实例C语言,实例之while循环
  16. 蓝桥杯java历年真题及答案整理21~40
  17. 方舟服务器商店系统怎么弄,方舟生存进化怎么设置商店系统
  18. Windows Mobile 6.x 下改Tahoma字体+微软雅黑说明
  19. FilterListener(超详细)
  20. 微信原图暴露的只能是 Exif ,你的隐私不在这!!!

热门文章

  1. windows和linux的协议栈驱动
  2. head first系列PDF资源
  3. c#事务的使用、示例及注意事项(转)
  4. [2018HN省队集训D8T1] 杀毒软件
  5. StarUML 破解方法2.X(转)
  6. 史上最简单MySQL教程详解(进阶篇)之存储引擎介绍及默认引擎设置
  7. 22)PHP,数组排序函数
  8. 循序渐进学Python2变量与输入
  9. 在Windows的CMD中如何设置支持UTF8编码?
  10. java中的exception stack有时候不输出的原因