Questions tagged [cyclomatic-complexity]

Cyclomatic complexity is a number used to express the complexity of source code (e.g. of a method). It is calculated based on the number of different possible paths through the source code. Low cyclomatic complexity is one factor to improve readability, maintainability, and testability of code.

The Cyclomatic Complexity (or conditional complexity) graphs theoretical software metric to measure the complexity of a program (or method, function, procedure ..). It was defined by Thomas J. McCabe in 1976.

Is calculated using the following formula:

V(G) = P + 1

where V(G) = Cyclomatic Complexity
P = number of decision predicate nodes (if, else, while, etc)

I.e. initially the cyclomatic complexity of each method is defined as 1. Each control flow instruction adds 1 to the complexity number of a method.

Since there is some correlation between good code quality and a low number of complexity, methods with a high Cyclomatic Complexity (e.g. 10 to 15) are considered hard to read and understand. Low number of Cyclomatic Complexity is a good indicator for readable, (re-)usable, reliabe and functional coding style. Because of this, it is recommended to split such methods with a high complexity into smaller ones.

For measuring the needed number of unit tests to get full code coverage it's better to use the Extended Cyclomatic Complexity or the NPath Complexity. Therefore McCabb’s Cyclomatic Complexity is not accurate enough, because it fails to distinguish between different conditional statements (control flow structures). It also does not consider nesting level of various control flow structures.

293 questions
1
vote
3 answers

Reduce the Cyclomatic complexity of a function

I have a function which uses the JPA Criteria API to retrieve data from the database depending on some params I pass to it. The params are passed using the object gridParams, as following : public List find(final GridParamsDTO…
Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
1
vote
1 answer

Reducing complexity of large switch statements

In the codebase I'm currently working on, it's common to have to take a string passed in from further up the chain and use it as a key to find a different String. The current standard idiom is to use switch statements, however for larger switch…
1
vote
0 answers

Populate POJO from Excel row

I am using Apache POI to read an Excel file having many columns. I have a POJO in which there is a field for each column in Excel sheet. How do I populate my POJO with values from Excel achieving minimum cyclomatic complexity? I had implemented…
Akshay Lokur
  • 6,680
  • 13
  • 43
  • 62
1
vote
4 answers

Reduce the cyclometic complexity for multiple if else statements

I have a function which accepts a Textfield as parameter and based on the tag, the number changes; Here is the code sample func textFieldDidChange(_ textField: UITextField) { if(textField.tag == 0){ …
1
vote
1 answer

Cyclomatic Complexity - Delphi API

I am searching for a Cyclomatic Complexity Api in Delphi (2010). I need to create a program that will analize a source code and report the Cyclomatic Complexity of all methods in all classes (just like SourceMonitor does). I cant use other…
Rafael Colucci
  • 6,018
  • 4
  • 52
  • 121
1
vote
1 answer

Reduce method complexity by removing foreach loops

I have the following code in c#: foreach (var c1 in object1.Collection1) { foreach (var c2 in c1.Collection2.Where(b => b.Settings?.Name != null)) { foreach (var c3 in c2.Settings.Name.Where(s =>…
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
1
vote
1 answer

Should I unit test methods that have a cyclomatic complexity of 1 if they are part of larger interactions?

I'm kind of new to unit testing so please excuse a naive question. I've heard the idea of not testing anything that has a cyclomatic complexity of 1. This is interesting to me b/c I do a lot of interaction testing between events. For example, button…
Trevor
  • 4,620
  • 2
  • 28
  • 37
1
vote
1 answer

Reduce cyclomatic complexity of a function

This function checks if the cell of a scrabble board is a double letter bonus. It has a 23 cyclomatic complexity which is higher than the threshold of 20. I don't know how to do it another way, I think that this is the only way to do it. Here is my…
freeNinja
  • 365
  • 1
  • 4
  • 11
1
vote
1 answer

Cyclomatic Complexity when I want to check the correct values of the arguments

I have a parameter with 3 parameters and i want to ensure that data from parameters are correct, so I have something like that: if param1 is null throw exception ArgumentNullException if param1.Property < 0 throw exception if para1.Property > 100…
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
1
vote
3 answers

How to reduce cyclomatic complexity in a if condition?

In an if condition as following: if( condition1 || condition2 || condition3 || condition4 || condition5) Where conditions are independent of each other, the code complexity tends to be high, is there a way to refactor this logic to reduce…
venkat g
  • 421
  • 1
  • 6
  • 20
1
vote
1 answer

Sanity check: Do these nested .All calls equate to the corresponding .Where and .SelectMany calls in LINQ?

I have the following code: bool b = myList .All(x => x.MyList .Where(y => y.MyBool) .All(y => y.MyList .All(z => z.MyBool))) Is this functionally equivalent to: bool b = myList .SelectMany(x => x.MyList) …
Neo
  • 4,145
  • 6
  • 53
  • 76
1
vote
0 answers

Cyclomatic complexity for nested IFs

While checking several descriptions/blogs/tutorials, I still don’t get how to calculate the McCabe metric for nested IFs. For example for the following pseudo code. IF (CHECK_A() && CHECK_B() && CHECK_C()) { DO_SOMETHING() } This code can be…
Max Frank
  • 11
  • 2
1
vote
1 answer

Python: reducing cyclomatic complexity

I need help in reducing the cyclomatic complexity of the following code: def avg_title_vec(record, lookup): avg_vec = [] word_vectors = [] for tag in record['all_titles']: titles = clean_token(tag).split() for word in…
futurenext110
  • 1,991
  • 6
  • 26
  • 31
1
vote
2 answers

How procced cyclomatic complexity in such javascript code?

i have some problems with complexity in my javascript. It looks like: function mainFunction(scope, element) { var eventHandlerMap = { 'firstEvent': firstEventHandler, 'secondEvent': secondEventHandler, …
1
vote
1 answer

Cyclomatic complexity calculation - concrete example

Let's suppose we have the following code: public void testIt(boolean a, boolean b){ if (a){ ... } if (b){ .... } } As I know there are three methods to calculate. I will use two of them: region formula and the rule of thumb. Using…
Pavel_K
  • 10,748
  • 13
  • 73
  • 186