ICPs are code elements that could impact in one's understanding. Example of ICPs include:
- Code branches (e.g., if–else or switch-case);
- Loops (e.g., for or while);
- Exception handlers (e.g., try-catch).
In isolation, these code elements may not hinder the understanding of a code unit (like a class). However, if used frequently in the same unit of code, they can potentially make it harder to understand.
Currently, this plugin computes the ICPs highlighted below.
Computes the number of methods within a class.
public Long getId() {
return id;
}
public getEmail() {
return email;
}
public String getName() {
return name;
}In the above example, we found three methods. We do not count constructs or automatic generated methods (such as equals and hashcode). Optinonaly, we could count equals and hashcode, if we add the "methodsAutoGenerated": true flag in the cdd.json config file.
Computes the number of annotation within a class.
@Table(name = "student")
@Entity
public class Student {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
@NotBlank
@Email
@Column(unique = true)
private String email;
@NotBlank
@Column(nullable = false)
private String name;
//...
}In the example above, although there are nine annotations, the tool will count only 7 (seven), because the counting is done only the first time an annotation is used. Subsequent use of the same annotation does not increase the number of this ICPs.
All uses of annotation are computed, including: class annotations, method annotations, annotation in class variables, and annotations in method parameters.
Computes the number of ifs and elses within a class.
if (a > b) {
// ...
} else if (b > c) {
// ...
} else if (c > d) {
// ...
} else {
// ...
}In this example, the tool would report four ICPs (one if, two else if, and one else).
This ICP also Computes the number of ternary operators.
String result = (e > 40) ? "pass" : "fail";
boolean x = (1 > 0) ? false : true;This ternary example counts four ICPs.
In an analog way, this ICP Computes the number of switchs and their cases within a class.
Contabiliza o número condições dentro de um if.
if (a > b && b < c || c > d) {
// code block
}The code above results in four ICPs: 1 for the if plus other three, one for each condition.
Computes the number of try, catch and finally statements within a class.
try {
// code block
} catch (Exception | CDDException e) {
// code block
} finally {
// code block
}In this example, our tool would count three ICPs: one for the try, one for the catch, and one for the finally.
The decision to count only 1 ICP, even using multicatch, is due to the fact that you don't open a new branch in the code to handle these two exceptions.
On the other hand, if we had two separated catchs, like in the next example, we would count two ICPs, one for each catch block. Overall, the code below would result in four ICPs.
try {
// code block
} catch (CDDException e) {
// code block
} catch (Exception e) {
// code block
} finally {
// code block
}Computes the number of throws within try blocks.
Computes the number of while and do-while loops within a class.
Computes the number of for loops within a class.
Computes the number of foreach loops within a class.
Computes the number of lambda expression within a class.
Computes the number of yield statements within a switch block.
Computes the number of super calls within a class.
Computes the number of anonimous classes within a class;
Computes the number of local variables defined within a class.
- TYPE_CAST_EXPRESSION("type_cast_expression")
- IMPLICIT_VARIABLE("implicit_variable")
- IMPORT_STATIC_REFERENCE_ELEMENT("import_static_reference_element")
- C_LASS_INITIALIZER("c_lass_initializer"),
- CLASS("class")
- METHOD_CALL_EXPRESSION("method_call_expression")