![Page 1: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/1.jpg)
Measuring Maintainability Dennis de Greef
![Page 2: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/2.jpg)
Dennis de Greef Software Developer at TransIP Meetup addict
Tech enthousiast Occasional speaker Bass player
Domotica Software architect Hardware hacker Infrastructure engineer
![Page 3: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/3.jpg)
Talk roadmap - About static software analysis - Measuring lines of code - Measuring complexity - Measuring coupling
![Page 4: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/4.jpg)
About software statistics
![Page 5: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/5.jpg)
Measuring Lines of Code
![Page 6: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/6.jpg)
How many lines of code? 1 <?php 2 3 /* this is a for-loop */ 4 for ($i = 0; $i < 100; $i++) 5 { 6 printf("hello"); 7 }
![Page 7: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/7.jpg)
How many lines of code? 1 <?php 2 3 /** 4 * This is a for-loop 5 */ 6 for ($i = 0; $i < 100; $i++) printf("hello");
![Page 8: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/8.jpg)
Depends on your definition
![Page 9: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/9.jpg)
![Page 10: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/10.jpg)
LOC: Physical lines of code LLOC: Logical lines of code CLOC: Comment lines of code NCLOC: Non-comment lines of code
Different types:
![Page 11: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/11.jpg)
How many lines of code?
7 physical lines of code 3 logical lines of code 1 comment lines of code 6 non-comment lines of code
1 <?php 2 3 /* this is a for-loop */ 4 for ($i = 0; $i < 100; $i++) 5 { 6 printf("hello"); 7 }
![Page 12: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/12.jpg)
How many lines of code?
6 physical lines of code 3 logical lines of code 3 comment lines of code 3 non-comment lines of code
1 <?php 2 3 /** 4 * This is a for-loop 5 */ 6 for ($i = 0; $i < 100; $i++) printf("hello");
![Page 13: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/13.jpg)
Some statisticsLOC
PhysicalCLOC
CommentNCLOC
Non-CommentLLOCLogical
symfony-standard 371490 102161(27.50%)
269329(72.50%)
84262(22.68%)
zend-framework2 306097 112498(36.75%)
193599(63.25%)
56053(18.31%)
laravel-framework 78191 35156 (44.96%)
43035(55.04%)
11970 (15.31%)
cakephp 93455 42454 (45.43%)
51001 (54.57%)
17235 (18.44%)
phploc 2.1.2-17-gb6bfd40
PSR-2
PSR-2
PSR-2
PSR-2
![Page 14: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/14.jpg)
WARNING!Don’t use these statistics as a form of measuring quality or productivity!
People will just ‘game’ the system
*applies different codestyle*
![Page 15: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/15.jpg)
![Page 16: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/16.jpg)
Use it to gain more insight about the application
“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.”
--Bill Gates
![Page 17: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/17.jpg)
composer require phploc/phploc$ phploc loc1.php phploc 2.0.2 by Sebastian Bergmann.!
Size Lines of Code (LOC) 7 Comment Lines of Code (CLOC) 1 (14.29%) Non-Comment Lines of Code (NCLOC) 6 (85.71%) Logical Lines of Code (LLOC) 3 (42.86%) Classes 0 (0.00%) Average Class Length 0 Average Method Length 0 Functions 0 (0.00%) Average Function Length 0 Not in classes or functions 3 (100.00%)
![Page 18: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/18.jpg)
Measuring Complexity
• Cyclomatic Complexity • N-path Complexity
Complexity and quality are strongly related
![Page 19: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/19.jpg)
Cyclomatic Complexity“It is a quantitative measure of the number of
linearly independent paths through a program's source code.”
M = E − N + 2PE = the number of edges of the graph. N = the number of nodes of the graph. P = the number of connected components.
http://www.literateprogramming.com/mccabe.pdf
![Page 20: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/20.jpg)
![Page 21: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/21.jpg)
Simply put:Counting of decision trees: - function - for(each) - while - if - case
![Page 22: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/22.jpg)
Cyclomatic Complexity 1 <?php 2 3 function foo($a, $b) { 4 if($a) { 5 if($b) { 6 echo "Hello World"; 7 } 8 } 9 } 10
5 edges 4 nodes 1 compound
M = 5 − 4 + 2x1 = 3M = E − N + 2P
![Page 23: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/23.jpg)
Cyclomatic Complexity8 edges 7 nodes 1 compound
M = 8 − 7 + 2x1 = 3M = E − N + 2P
1 <?php 2 3 function foo() { 4 if($a) { 5 echo '1'; 6 } else { 7 echo '2'; 8 } 9 if($b) { 10 echo '3'; 11 } else { 12 echo '4'; 13 } 14 }
![Page 24: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/24.jpg)
Cyclomatic ComplexityHow many tests do we need?
M = 8 − 7 + 2x1 = 3
Complete branch coverage: foo(true, false) foo(false, true)
![Page 25: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/25.jpg)
Code Coverage 1 <?php 2 3 function foo($a, $b) { 4 if($a) { 5 // ... 6 } 7 8 if($b) { 9 // ... 10 } 11 } 12 13 foo(true, false); 14 foo(false, true);
All paths are tested with these two function calls. This implies 100% code coverage. !
Or does it? What about: foo(false, false) foo(true, true) ?
![Page 26: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/26.jpg)
Code CoveragePath coverage:
http://derickrethans.nl/path-branch-coverage.html
Xdebug 2.3.0
PHP_CodeCoverage: not supported
![Page 27: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/27.jpg)
N-path Complexity
“The NPath complexity of a method is the number of acyclic execution paths through that method.”
![Page 28: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/28.jpg)
“The NPath complexity of a method is the number of acyclic execution paths through that method.”
N-path Complexity
![Page 29: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/29.jpg)
“The NPath complexity of a method is the number of acyclic execution paths through that method.”
N-path Complexity
![Page 30: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/30.jpg)
N-path Complexity
Path coverage
M = E − N + 2PE = the number of edges of the graph. N = the number of nodes of the graph. P = the number of connected components.
![Page 31: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/31.jpg)
N-path Complexity
foo(false, false) foo(true, false) foo(false, true) foo(true, true)
Cyclomatic Complexity = 3 N-path Complexity = 4
N-path is exponential
![Page 32: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/32.jpg)
N-path Complexity
foo(false, false) foo(true, false) foo(false, true) foo(true, true)
Cyclomatic Complexity = 3 N-path Complexity = 4
N-path is exponential
Number of unique paths
![Page 33: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/33.jpg)
N-path Complexity // … $count['staticMethods'], $count['methods'] > 0 ? ($count['staticMethods'] / $count['methods']) * 100 : 0, $count['publicMethods'], $count['methods'] > 0 ? ($count['publicMethods'] / $count['methods']) * 100 : 0, $count['nonPublicMethods'], $count['methods'] > 0 ? ($count['nonPublicMethods'] / $count['methods']) * 100 : 0, $count['functions'], $count['namedFunctions'], $count['functions'] > 0 ? ($count['namedFunctions'] / $count['functions']) * 100 : 0, // …
![Page 34: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/34.jpg)
N-path Complexity
The method printResult() has an NPath complexity of 47683715820312500.
The configured NPath complexity threshold is 200.
![Page 35: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/35.jpg)
![Page 36: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/36.jpg)
Measuring Coupling
http://www.codemanship.co.uk/parlezuml/metrics/OO%20Design%20Principles%20&%20Metrics.pdf
Martin’s Metrics
![Page 37: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/37.jpg)
Measuring CouplingCoupling between packages
Packages being either a namespace or composer package
![Page 38: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/38.jpg)
Measuring Coupling
Afferent Coupling (Ca):"Amount of other packages depending on this class (incoming) !
Efferent Coupling (Ce):"Amount of dependencies of this class !
Instability (I):"Resilience to change, metric between 0 and 1. Where 0 is stable.
I = Ce / (Ce + Ca)
![Page 39: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/39.jpg)
Measuring Coupling
Ca = 2
Ce = 3
I = Ce / (Ce + Ca)
I = 3 / (3+2)
I = 0,6
I = 0,0 = Completely stableI = 1,0 = Completely unstable
![Page 40: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/40.jpg)
Measuring Coupling
Abstract:"Either an abstract class or interface !
Concrete:"Any class that is neither abstract or final !
Abstractness (A):"How abstract a package is, ratio between 0 and 1. Where 0 is concrete.
A = Abstract / (Abstract + Concrete)
![Page 41: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/41.jpg)
Measuring CouplingPHPMD (PHP Mess Detector)
Can detect (code size rules): - Cyclomatic complexity - N-path complexity - Excessive method length - Too many methods - etcetera
1020010010
And other things- Clean Code - Design Rules - Naming Rules - Unused Code Rules
![Page 42: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/42.jpg)
Integrating it all
http://jenkins-php.org/
![Page 43: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/43.jpg)
Questions?
![Page 44: Measuring maintainability; software metrics explained](https://reader034.vdocuments.net/reader034/viewer/2022052401/55d39407bb61eb175d8b4824/html5/thumbnails/44.jpg)
Thank youBig thanks to: !
Derick Rethans (Xdebug) Sebastian Bergmann"(PHPLOC, PHPunit, PHP_CodeCoverage) Thomas J. McCabe Sr: Cyclomatic Complexity Robert C. Martin: Lots of things!(nicknamed ‘Uncle Bob’) !
Many more…
Twitter: @dennisdegreef Blog: dennisdegreef.net IRC (Freenode): link0 Github: dennisdegreef / link0 PHPNL-Slack: link0 Joind.in: https://joind.in/14991