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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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 Marketplace Apps & Integrations

Happy New Year and Welcome New Marketplace Partner Program Partners!

Happy New Year! We hope you all had a safe and restful holiday season. 2020 was a unique year full of unforeseen events; however, as we enter the new year of 2021, we’re optimistic for the light at t...

509 views 7 18
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