Use Source Monitor to detect the loop complexity of Java code-Alibaba Cloud Developer Community

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:

Please read this disclaimer carefully before you start to use the service. By using the service, you acknowledge that you have agreed to and accepted the content of this disclaimer in full. You may choose not to use the service if you do not agree to this disclaimer. This document is automatically generated based on public content on the Internet captured by Machine Learning Platform for AI. The copyright of the information in this document, such as web pages, images, and data, belongs to their respective author and publisher. Such automatically generated content does not reflect the views or opinions of Alibaba Cloud. It is your responsibility to determine the legality, accuracy, authenticity, practicality, and completeness of the content. We recommend that you consult a professional if you have any doubt in this regard. Alibaba Cloud accepts no responsibility for any consequences on account of your use of the content without verification. If you have feedback or you find that this document uses some content in which you have rights and interests, please contact us through this link: We will handle the matter according to relevant regulations.
Selected, One-Stop Store for Enterprise Applications
Support various scenarios to meet companies' needs at different stages of development

Start Building Today with a Free Trial to 50+ Products

Learn and experience the power of Alibaba Cloud.

Sign Up Now