【问题标题】:Cyclomatic Complexity OF Model Class模型类的圈复杂度
【发布时间】:2020-02-29 19:54:32
【问题描述】:

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

请查找代码 sn-p 为:

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
}

【问题讨论】:

标签: java static-analysis code-metrics cyclomatic-complexity


【解决方案1】:

根据Wikipedia page

[圈]复杂度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 条边的图(没有connected components),复杂度为 1。

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

【讨论】:

  • 谢谢你这么好的解释。但是我可以减少它吗?
  • 当然。减少字段数。摆脱任何不需要的设置器。但在你这样做之前,问问自己通过降低圈复杂度你将实际实现什么。请记住,CC 只是一种用于识别可能是可维护性问题的代码的启发式方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-29
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多