3

I read about Cyclomatic complexity and multiple return statements, but I am a little bit confused, because of the different opinions for multiple return statements.

First of all, during Cyclomatic Complexity calculation should I count each return statement as an endpoint which increase the complexity I think? In the formula (M = E - N + 2*P) when I add a return statement, it increases by one, is it right?

Guard clauses, which are used for simple sanity checks adds is another way instead of nested if clauses in order to return as soon as possible. However, this adds more return stetaments to the code and increase CC?

Are there any common best practices for usage of guard clauses and multiple return statements in terms of CC?

Deniz
  • 858
  • 10
  • 31
  • I think it's an interesing question. However, consider re-wording it - some people will not like "What is your opinion about..." and will flag your question as "opinion based". Make it more clear that you are not asking about opinion, but about facts :) – Honza Zidek May 21 '14 at 12:14
  • That's what I thought while I was writing the question. :) Thanks Honza. – Deniz May 21 '14 at 12:18
  • Using ifs will not help you, as they also increase cyclomatic complexity, as it measures different possible ways through your code. – Uwe Allner May 21 '14 at 12:21
  • Uwe, you are right, but we do not know the path of the call flow in advance (depends on parameters and etc.). As far as my understanding CC is not interested in something like best/average/worst case analysis (from computation complexity). You said "as it measures different possible ways through your code", but this is sounds like an assumption more than a formula (I mean if you have an invalid parameter you have return as soon as possible, but what if the data is valid?). – Deniz May 21 '14 at 12:27
  • The calculation of CC can not know, how the flow through the code will be, it just measures possibilities (not even probabilities like "complexity is lower, when you leave the code early). It is about testability and mainability (a lot of -bilities ;o), as you have to cover **all** ways e.g. in your unit tests, independent of how often they may be used. It counts that they could be used. – Uwe Allner May 21 '14 at 12:35
  • possible duplicate of [Cyclomatic Complexity in piece of code with multiple exit points](http://stackoverflow.com/questions/15391845/cyclomatic-complexity-in-piece-of-code-with-multiple-exit-points) – Dave Schweisguth May 21 '14 at 13:37

1 Answers1

1

Although many metrics have been suggested, software complexity turns out to be not easily measurable. Despite being very commonly used, cyclomatic complexity has its limitations too. Below are references to some academic criticisms.

To give a concrete answer to the question, I do not know of any such best practice. I must say I regard CC as a rough indicator at best on my part. Keeping guard conditions would be more important to me. Hope this helps.

henginy
  • 2,041
  • 1
  • 16
  • 27