Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How does OpenClover count complexity for method?

Alexey Ulasevich January 20, 2020

I have a method in a class:

    public boolean tryFile() throws IOException {
File file = File.createTempFile("test", "dat");
file.deleteOnExit(); try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(20); }
return file.exists(); }

According OpenClover documentation:

Method Complexity

Cyclomatic complexity of a single method. It's calculated as follows:

empty method complexity == 1
simple statement complexity == 0
switch block complexity == number of case statements
try catch block complexity == number of catch statements
ternary expression complexity == 1
boolean expression complexity == number of && or || in expression

In report generated by OpenClover (version 4.4.1) the complexity for this method equals 3. Why? How it was counted?

PS. For example in JaCoCo report I see Ctx=1 for this method.

1 answer

0 votes
Marek Parfianowicz
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 6, 2020

From https://github.com/openclover/clover/blob/master/clover-core/src/main/java/com/atlassian/clover/instr/java/java.g

tryCatchBlock [boolean labelled] returns [CloverToken last]
{
last = null;
int complexity = 0;
ContextSet saveContext = getCurrentContext();
}
: tr:"try" (lp:LPAREN {insertAutoCloseableClassDecl((CloverToken)tr);} declaration {complexity++; instrArmDecl(((CloverToken)lp).getNext(), (CloverToken)LT(0), saveContext);} (semi:SEMI declaration {complexity++; instrArmDecl(((CloverToken)semi).getNext(), (CloverToken)LT(0), saveContext);})* (SEMI)? rp:RPAREN )?
{enterContext(ContextStore.CONTEXT_TRY); saveContext = getCurrentContext();}
last=compoundStatement
{exitContext();}
(last=handler {complexity++;})*
( "finally"
{enterContext(ContextStore.CONTEXT_FINALLY); saveContext = getCurrentContext();}
last=compoundStatement
{exitContext();}
)?
{ if (!labelled) {
instrInlineBefore((CloverToken)tr, last, saveContext, complexity);
}
}
;

 

* 1st comes from the try-with-resources block:

try (FileOutputStream fos = new FileOutputStream(file))

* 2nd comes from the method entry as per docs

I'm not sure about the 3rd one...

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events