Compare the complexities of the two pieces of code A & B using these two metrics LOC and cyclomatic-complexity. What conclusions can you draw about the relative complexity of the code?
Code A |
Code B |
int i = 1;while(i < =5){
playACard(i); if (playerHasWon(i)) break; i++ } |
int j = 0;int i = 2;
j = i; j = j + i; j++; System.out.println (j); System.out.println |
Code A and B both have the same amount of LOC which is 7 according to the LOC metric which counts the physical Lines Of Code.
Code A has a cyclometric-complexity of 3 (1 for if, 1 for while loop and 1 for the whole method), while code B has a complexity of 1 (1 for the method). Code A is more complex than code B according to the cyclomatic-complexity metric as code A has a while loop and an if statement while code B is linear.
Reference:
Cole, R. (2007) Cyclomatic Complexity and Unit Tests. [online] Available at: http://www.rkcole.com/articles/other/CodeMetrics-CCN.html [Accessed: 15 Apr 2013].
En.wikipedia.org (1976) Cyclomatic complexity – Wikipedia, the free encyclopedia. [online] Available at: http://en.wikipedia.org/wiki/Cyclomatic_complexity [Accessed: 15 Apr 2013].
En.wikipedia.org (2012) Source lines of code – Wikipedia, the free encyclopedia. [online] Available at: http://en.wikipedia.org/wiki/Source_lines_of_code [Accessed: 15 Apr 2013]