Saturday, June 13, 2015

Example of Cyclomatic complexity analysis over real projects

Processed by:
1) launch of https://github.com/checkstyle/contribution/tree/master/checkstyle-tester generate HTML report

2) html2text target/site/index.html | grep "Cyclomatic Complexity is" | cut -d' ' -f7 | sort | uniq --count | sort -h -k 2

3) % is calculated as "=TRUNC(((B2)/B$47)*100)"

So amount of methods the complicated then 10 (from all method that complicated than 5) in the project is about 5%.  Below is tables for: Checkstyle , Spring Framework, Open JDK 7.

Checkstyle 6.7 Cynclomatic complexity analysis:

               CC           Count                  %
5 130 15
6 83 12
7 66 9
8 52 10
9 53 4
10 22 4
11 23 4
12 25 1
13 7 0
14 3 1
15 8 0
16 4 0
17 2 0
18 5 1
19 10 0
20 1 2
21 13 0
22 1 0
23 5 0
24 1 0
27 1 0
29 1 0
30 1 0
32 1 0
34 2 0
44 2 0
47 2 0
81 1 100

Total: 525


Spring frameworks v4.1.6.RELEASE Cyclomatic complexity analysis:
               CC           Count                 %
5 746 30
6 450 18
7 312 12
8 215 8
9 157 6
10 109 4
11 80 3
12 55 2
13 52 2
14 34 1
15 33 1
16 25 1
17 18 0
18 19 0
19 14 0
20 17 0
21 11 0
22 8 0
23 9 0
24 6 0
25 7 0
26 11 0
27 1 0
28 2 0
29 1 0
31 3 0
32 2 0
33 5 0
34 3 0
35 2 0
36 3 0
37 1 0
38 1 0
40 2 0
41 1 0
44 1 0
47 1 0
48 1 0
50 2 0
55 1 0
60 1 0
61 1 0
81 2 0
127 1 0
184 1 0

Total:2427

OpenJdk7 Cyclomatic complexity analysis: 

               CC           Count                 %
5 4650 25
6 2947 16
7 2234 12
8 1542 8
9 1161 6
10 932 5
11 706 3
12 574 3
13 505 2
14 383 2
15 332 1
16 261 1
17 230 1
18 200 1
19 183 1
20 128 0
21 119 0
22 121 0
23 102 0
24 78 0
25 58 0
26 60 0
27 57 0
28 47 0
29 30 0
30 29 0
31 27 0
32 30 0
33 22 0
34 30 0
35 11 0
36 19 0
37 27 0
38 12 0
39 17 0
40 10 0
41 16 0
42 12 0
43 11 0
44 14 0
45 6 0
46 11 0
47 14 0
48 9 0
49 7 0
50 12 0
51 11 0
52 6 0
53 6 0
54 8 0
55 6 0
56 5 0
57 2 0
58 4 0
59 1 0
60 3 0
62 6 0
63 3 0
64 4 0
66 4 0
67 6 0
68 2 0
69 1 0
70 2 0
71 1 0
72 2 0
73 3 0
74 7 0
75 4 0
76 3 0
77 3 0
78 2 0
79 1 0
80 4 0
81 1 0
82 5 0
83 1 0
85 1 0
86 2 0
87 1 0
88 3 0
93 2 0
95 3 0
96 1 0
97 1 0
100 1 0
101 1 0
103 1 0
109 1 0
110 1 0
111 1 0
121 1 0
124 1 0
125 1 0
130 2 0
140 1 0
145 1 0
149 1 0
164 1 0
186 1 0
219 1 0
256 1 0
281 1 0

Total:18136

Understanding of Cyclomatic Complexity for measuring code quality

But does that mean a program with a high Cyclomatic Complexity (CC) has a bad quality ? For sure not !

It is just another metrics and some people think it is better to use Line numbers instead as more simple and more robust metric. Very good paper  - http://www.leshatton.org/Documents/TAIC2008-29-08-2008.pdf#17 (page 17) , see also Conclusion - page 45.

It is common when people say " ... reducing the cyclomatic complexity of the code can help reducing the number of test cases ... " - that is not always true!!
Blind following of Cyclomatic metric will lead you to code that have a lot of small methods that will be hard to maintain as encapsulation might be damaged. It is not a rare case there you should put ignore to some case where complexity is high as you defined for the rest of the project.
To keep all that logic(even a bit complicated) in one method to not let other methods use it and what is more problematic change them to their needs. Not everything should be decomposed to smaller parts.
Finally if your complicated method is "public" and you will decompose it internals for several "private" method - you will not reduce amount of tests !!! as in tests you will still check your public method with all possible values in arguments.

The main point of CC is to measure complexity to measure amount of tests, so it suppose to be used as (from wiki) :
 branch coverage \leq cyclomatic complexity \leq number of paths

from wiki:
McCabe showed that the cyclomatic complexity of any structured program with only one entrance point and one exit point is equal to the number of decision points (π) (i.e., "if" statements or conditional loops) contained in that program plus one. However, this is true only for decision points counted at the lowest, machine-level instructions. Decisions involving compound predicates like those found in high-level languages like IF cond1 AND cond2 THEN ... should be counted in terms of predicate variables involved, i.e. in this examples one should count two decision points, because at machine level it is equivalent to  IF cond1 THEN IF cond2 THEN ... 
Cyclomatic complexity may be extended to a program with multiple exit points; in this case it is equal to:
π − s + 2, 
where π is the number of decision points in the program, and s is the number of exit points

So amount of decision point is good measure of complexity ,  but as you can see in definition it could be reduced by adding more exit points. That is true for amount of test calculation  but that is not true to measure of code quality and readability.
So "multi exits/returns" could be a incentive for engineers to calm down Checkstyle or PMD . But that conflict with best practice to have one entry and one exit point in method.

Conclusion: Cyclomatic Complexity is for measuring of Tests amount and NOT about code quality. But CC could be used as quality measure with custom to project level/threshold selection. Do not be ashamed to have complexity level 15 or even more, but keep it below 20 to catch really bad designed code automatically.

I highly recommend to read https://www.cqse.eu/en/blog/mccabe-cyclomatic-complexity/ to see on code examples how weird complexity calculation could be.

Checkstyle's HTML documentation was updated for this metric: http://checkstyle.sourceforge.net/config_metrics.html#CyclomaticComplexity

I updated Chekstyle code to follow level less then 11, I can confirm that about 54 cases were real ugly and messy code and it  was good that that metrics is pointed to that code. But in non library code level 11 is too demanding but reachable, 15 is ok.

Example of Cyclomatic complexity calculation over code base of Checkstyle, Spring, OpenJDK.

Sources:
https://en.wikipedia.org/wiki/Cyclomatic_complexity (especially links to external and other ref)
http://damienlepage.com/cyclomatic-complexity/
http://www.sonarqube.org/discussing-cyclomatic-complexity/


Wednesday, June 10, 2015

Sync tags from Zimbra to Thunderbird

To Sync tags from Zimbra to Thunderbird please make sure that your tags are named exactly as it on Zimbra (manual sync).
If you rename a tag please update ~/.thunderbird/rnwdyrj0.default/prefs.js file manually.

incorrect variant (after successful renaming "work" to "todo" in Thunderbird):

user_pref("mailnews.tags.work.color", "#3333FF");
user_pref("mailnews.tags.work.tag", "todo");

incorrect variant :

user_pref("mailnews.tags.todo.color", "#3333FF");
user_pref("mailnews.tags.todo.tag", "todo");