1

Can anybody help me to understand why my model class has cyclomatic complexity of 89. When I run PMD it gives me 'The class 'GetResponse' has a total cyclomatic complexity of 89 (highest 1)'.

Please find code snippet as:

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
}
Linus Fernandes
  • 498
  • 5
  • 30
Priyanka
  • 11
  • 4

1 Answers1

2

According the Wikipedia page:

The [cyclomatic] complexity M is [] defined as

M = E − N + 2P,

where

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

By my count, your class has 44 fields plus (I assume) getters and setters for each field.

A typical getter looks like this:

  public T getX() { return x; }

and this has cyclomatic complexity 1.

A typical setter looks like this:

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

and this also has cyclomatic complexity 1.

Finally, if we consider the field declarations as a sequence of statements, that is a graph with 44 nodes and 43 edges (and no connected components), giving a complexity of 1.

Thus, the aggregated cyclomatic complexity of the entire class is 44 x 1 + 44 x 1 + 1 == 89.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • thanks for such a nice explanation.But can I reduce it? – Priyanka Nov 05 '19 at 06:28
  • Sure. Reduce the number of fields. Get rid of any setters that are not required. But before you do that, ask yourself what you will actually achieve by reducing the cyclomatic complexity. Bear in mind that CC is *just* a heuristic for identifying code that *may be* a problem for maintainability. – Stephen C Nov 05 '19 at 06:59