Does Your Code Measure Up? By:
Adam Culp Twitter: @adamculp
Slide 2
Does Your Code Measure Up? About me
●
2
–
OSS Contributor
–
PHP Certified
–
Zend Certification Advisory Board
–
PHP-Fig voting member (IBM i Toolkit)
–
Beachcasts and RunGeekRadio Host
–
Consultant at Zend Technologies
–
Organizer SoFloPHP (South Florida)
–
Organizer SunshinePHP (Miami)
–
Long distance (ultra) runner
–
Photography Enthusiast
–
Judo Black Belt Instructor
Slide 3
Does Your Code Measure Up? About me
●
–
OSS Contributor
–
PHP Certified
–
Zend Certification Advisory Board
–
PHP-Fig voting member (IBM i Toolkit)
–
Beachcasts and RunGeekRadio Host
–
Consultant at Zend Technologies
–
Organizer SoFloPHP (South Florida)
–
Organizer SunshinePHP (Miami)
–
Long distance (ultra) runner
– –
3
I am the Photography Enthusiast PHP Ninja!!! Judo Black Belt Instructor
Slide 4
Does Your Code Measure Up? ●
Fan of iteration –
4
Pretty much everything requires iteration to do well: ●
Long distance running
●
Judo
●
Development
●
Evading project managers
●
Code Quality!
Slide 5
Does Your Code Measure Up? ●
Metrics –
5
Not productive
Slide 6
Does Your Code Measure Up? ●
Metrics –
6
More accurate, quantifiable
Slide 7
Does Your Code Measure Up? ●
Why Measure? –
7
Slide 8
Does Your Code Measure Up? ●
Why Measure? –
8
Highlight bugs
Slide 9
Does Your Code Measure Up? ●
9
Why Measure? –
Highlight bugs
–
Improve quality ●
Easier onboarding
●
Less reading, more writing
●
Testable
Slide 10
Does Your Code Measure Up? ●
Why Measure? –
Highlight bugs
–
Improve quality
–
10
●
Easier onboarding
●
Less reading, more writing
●
Testable
Satisfied customers ●
Faster development
●
Less broken
Slide 11
Does Your Code Measure Up? ●
Why Measure? –
Highlight bugs
–
Improve quality
–
–
11
●
Easier onboarding
●
Less reading, more writing
●
Testable
Satisfied customers ●
Faster development
●
Less broken
Personal pride
Slide 12
Does Your Code Measure Up? ●
Why Measure? –
Highlight bugs
–
Improve quality
–
12
●
Easier onboarding
●
Less reading, more writing
●
Testable
Satisfied customers ●
Faster development
●
Less broken
–
Personal pride
–
Higher salary
Slide 13
Does Your Code Measure Up? ●
Bad Music –
13
Not a song writer
Slide 14
Does Your Code Measure Up? ●
14
Bad Music –
Not a song writer
–
“By ear” not reliable
Slide 15
Does Your Code Measure Up? ●
Code “smells” –
15
What are “smells”? ●
Indications of spoiled code nearby
●
Not conclusive
●
The “smell” is not bad
Slide 16
Does Your Code Measure Up? ●
What To Measure? –
16
Slide 17
Does Your Code Measure Up? ●
What To Measure? –
Cyclomatic complexity ●
17
“The count of the number of linearly independent paths through the source code.” - wikipedia
●
Decision points
●
Less than 10 (personally less than 6)
Slide 18
Does Your Code Measure Up? ●
18
Cyclomatic Complexity Example
Slide 19
Does Your Code Measure Up? ●
What To Measure? –
Cyclomatic complexity
–
Duplicate code ●
19
Rule of 3
Slide 20
Does Your Code Measure Up? ●
20
What To Measure? –
Cyclomatic complexity
–
Duplicate code
–
Long classes ●
Less than 1,000 lines
●
Classes solve a (1) problem
Slide 21
Does Your Code Measure Up? ●
What To Measure? –
Cyclomatic complexity
–
Duplicate code
–
Long classes
–
Class Complexity ●
21
Less than 50
Slide 22
Does Your Code Measure Up? ●
22
What To Measure? –
Cyclomatic complexity
–
Duplicate code
–
Long classes
–
Class complexity
–
Long methods ●
Less than 100 (personally less than 20)
●
Method should do one thing
Slide 23
Does Your Code Measure Up? ●
23
What To Measure? –
Cyclomatic complexity
–
Duplicate code
–
Long classes
–
Class complexity
–
Long methods
–
Unused variables
Slide 24
Does Your Code Measure Up? ●
What To Measure? –
Cyclomatic complexity
–
Duplicate code
–
Long classes
–
Class complexity
–
Long methods
–
Unused variables
–
Lack or overuse of comments ●
24
Clear, concise, not explain bad code
Slide 25
Does Your Code Measure Up? ●
25
Comment Example
Slide 26
Does Your Code Measure Up? ●
26
What To Measure? –
Cyclomatic complexity
–
Duplicate code
–
Long classes
–
Class complexity
–
Long methods
–
Unused variables
–
Lack or overuse of comments
–
Global usage
Slide 27
Does Your Code Measure Up? ●
27
What To Measure? –
Cyclomatic complexity
–
Duplicate code
–
Long classes
–
Class complexity
–
Long methods
–
Unused variables
–
Lack or overuse of comments
–
Global usage
–
Npath complexity ●
Possible paths through code
●
Less than 200 paths
Slide 28
Does Your Code Measure Up? ●
What To Measure? –
Cyclomatic complexity
–
Duplicate code
–
Long classes
–
Class complexity
–
Long methods
–
Unused variables
–
Lack or overuse of comments
–
Global usage
–
Npath complexity
–
Much, much, more ●
28
Code smells
Slide 29
Does Your Code Measure Up? ●
Tools –
29
https://github.com/exakat/php-static-analysis-tools
Does Your Code Measure Up? ●
Docker Hub
https://hub.docker.com/u/adamculp/
31
Slide 32
Does Your Code Measure Up? ●
Tools –
https://github.com/exakat/php-static-analysis-tools ●
32
PHPLoc
Slide 33
Does Your Code Measure Up? ●
33
PHPLoc Results
Slide 34
Does Your Code Measure Up? ●
Tools –
34
https://github.com/exakat/php-static-analysis-tools ●
PHPLoc
●
PHP_Depend - https://pdepend.org/
Slide 35
Does Your Code Measure Up? ●
PHP_Depend Result –
Graphs and XML output
ANDC AHH
0.169
9.333 7.318 NOC 16.945 NOM LOC
NOP
CYCLO Low
35
0.585 0.442 342 3192 23359 395815 96646 66812 9891
Average
High
NOM
4.137 CALLS
0.102
FANOUT Generated by PHP_Depend
Slide 36
Does Your Code Measure Up? ●
Tools –
36
https://github.com/exakat/php-static-analysis-tools ●
PHPLoc
●
PHP_Depend
●
PHP Copy/Paste Detector
Slide 37
Does Your Code Measure Up? ●
37
PHP Copy/Paste Detector Result
Does Your Code Measure Up? ●
Tools –
https://github.com/exakat/php-static-analysis-tools ●
PHPLoc
●
PHP_Depend
●
PHP Copy/Paste Detector
●
PHP Mess Detector Codesize – Unused PHP Dead Code Detector –
●
–
41
Searches for code no longer used
Slide 42
Does Your Code Measure Up? ●
Tools –
https://github.com/exakat/php-static-analysis-tools
–
PHPMetrics ●
42
http://phpmetrics.org
Slide 43
Does Your Code Measure Up? ●
43
PHPMetrics Results
Slide 44
Does Your Code Measure Up? ●
44
Tools –
https://github.com/exakat/php-static-analysis-tools
–
PHPMetrics
–
PHP_Codesniffer ●
Create rules/sniffs to ensure standards are followed
●
From CLI, IDE, or via SCM hooks
Slide 45
Does Your Code Measure Up? ●
PHP Upgrading – Get to 7+ –
45
PHPCompatibility ●
Uses PHPCodesniffer
●
Outputs detailed results
Slide 46
Does Your Code Measure Up? ●
PHP Upgrading –
PHPCompatibility ●
46
Sample result
Slide 47
Does Your Code Measure Up? ●
Commercial Tools –
47
Scrutinizer
Slide 48
Does Your Code Measure Up? ●
Commercial Tools –
Scrutinizer
–
Zend Server and Z-Ray ●
48
Debugging, Code tracing, Error reporting
Slide 49
Does Your Code Measure Up? ●
49
Apache Bench Result
Slide 50
Does Your Code Measure Up? ●
50
Automate!
Slide 51
Does Your Code Measure Up? ●
51
Conclusion –
Measure all the things!
–
Don’t fear results, share them
–
Reduce complexity
–
Leave code cleaner than you got it
–
Learn to “smell” problems
–
Use refactoring to fix shortcomings
–
Love iteration!