首页 > 解决方案 > 模型类的圈复杂度

问题描述

任何人都可以帮助我理解为什么我的模型类的圈复杂度为 89。当我运行 PMD 时,它给了我“类 'GetResponse' 的总圈复杂度为 89(最高 1)”。

请找到代码片段:

public class GetResponse  {
private String ewbNo;
private String ewayBillDate;
private String genMode;
private String userGstin;
private String supplyType;
private String subSupplyType;
private String docType;
private String docNo;
private String docDate;
private String fromGstin;
private String fromTrdName;
private String fromAddr1;
private String fromAddr2;
private String fromPlace;
private String fromPincode;
private String fromStateCode;
private String toGstin;
private String toTrdName;
private String toAddr1;
private String toAddr2;
private String toPlace;
private String toPincode;
private String toStateCode;
private Float totalValue;
private Float totInvValue;
private Float cgstValue;
private Float sgstValue;
private Float igstValue;
private Float cessValue;
private String transporterId;
private String transporterName;
private String transDocNo;
private String transMode;
private String transDocDate;
private String status;
private Integer actualDist;
private Integer noValidDays;
private String validUpto;
private Integer extendedTimes;
private String rejectStatus;
private String vehicleType;
private String actFromStateCode;
private String actToStateCode;
private Object itemList;
private Object VehiclListDetails;

//getters and setters
}

标签: javastatic-analysiscode-metricscyclomatic-complexity

解决方案


根据维基百科页面

[圈] 复杂度 M 是 [] 定义为

M = E − N + 2P,

在哪里

E = the number of edges of the graph.
N = the number of nodes of the graph.
P = the number of connected components.

据我统计,您的班级有 44 个字段,以及(我假设)每个字段的 getter 和 setter。

典型的 getter 如下所示:

  public T getX() { return x; }

这具有圈复杂度1。

一个典型的 setter 如下所示:

  public void setX(T x) { this.x = x; }

这也有圈复杂度1。

最后,如果我们将字段声明视为一系列语句,即具有 44 个节点和 43 条边(并且没有连通分量)的图,复杂度为 1。

因此,整个类的聚合圈复杂度为44 x 1 + 44 x 1 + 1 == 89


推荐阅读