It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How does OpenClover count complexity for method? Edited

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

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
Community showcase
Published in Bamboo

Cannot save a repository using Chrome v83

Hello Bamboo Community, I'm Jey from Bamboo Support team. If you have upgraded your Chrome browser to version 83 recently, it is observed that the save operation just spins and never completes w...

73 views 1 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you