measuring your code 2.0
DESCRIPTION
One of the biggest problems of software projects is that, while the practice of software development is commonly thought of as engineering, it is inherently a creative discipline; hence, many things about it are hard to measure. While simple yardsticks like test coverage and cyclomatic complexity are important for code quality, what other metrics can we apply to answer questions about our code? What coding conventions or development practices can we implement to make our code easier to measure? We'll take a tour through some processes and tools you can implement to begin improving code quality in your team or organization, and see what a difference it makes to long-term project maintainability. More importantly, we'll look at how we can move beyond today's tools to answer higher-level questions of code quality. Can 'good code' be quantified?TRANSCRIPT
![Page 1: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/1.jpg)
Measuring your code
@nateabele
A talk by Nate Abele
![Page 3: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/3.jpg)
Confession
![Page 4: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/4.jpg)
![Page 5: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/5.jpg)
Coercion, red bars & broken windows
![Page 6: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/6.jpg)
Continuous
![Page 7: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/7.jpg)
Goal: Perfection
![Page 8: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/8.jpg)
Or at the least, continuous improvement
![Page 9: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/9.jpg)
a.k.a. 改善 (kaizen)
![Page 10: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/10.jpg)
What people usually think
Estimating costs
Projecting deadlines
Managerial BS!
![Page 11: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/11.jpg)
Client Spec SheetFlash intro with no load time
User account logins, password optional
Ajax chat
“Like Google”
(actual bullet points, some paraphrased)
![Page 12: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/12.jpg)
...and my personal favorite
Social network
![Page 13: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/13.jpg)
Measurement is an essential element of management; there is little chance of controlling what we can not measure.
- Wikipedia, “Software metric”
![Page 14: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/14.jpg)
Wherefore... (WTF) ?
![Page 15: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/15.jpg)
“Engineer” & “architect”
![Page 16: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/16.jpg)
![Page 17: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/17.jpg)
Cognitive DissonanceEngineers deal with tangible, immutable constraints, like gravity
The practice of developing software is an inherently creative discipline
*
* Thank you, Jones
![Page 18: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/18.jpg)
Cognitive DissonanceDeveloper constraints (scope, schedule, budget) potentially often in flux
Software is inter-related; working on one part changes the others
No project is exactly the same as another
/
![Page 19: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/19.jpg)
ConclusionIt’s not useful to measure high-level, intangible things like whole projects
This is where scrum comes in handy
Instead, we can use lower-level, more concrete measurements
![Page 20: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/20.jpg)
What can we measure?
![Page 21: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/21.jpg)
Code!!
![Page 22: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/22.jpg)
More specifically...Unit test coverage
Complexity
Speed
Documentation
![Page 23: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/23.jpg)
More specifically...
Standards conformance
Refactoration!
![Page 24: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/24.jpg)
Backing up...What is a metric?
You can cheat and use booleans, too
Measurement assigns numbers based on well-defined meaning
- Sometimes the environment must be modified
- Special development procedures that track various activities - Wikipedia (paraphrased)
![Page 25: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/25.jpg)
Notes on continuous integrationA build system
Runs on every code commit
Runs tests
Reports
![Page 26: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/26.jpg)
Metric examples
![Page 27: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/27.jpg)
PHP Code Sniffer
PEAR Package:http://pear.php.net/package/PHP_CodeSniffer
Checks conformance of a set of files against a series of classes called “sniffs”
![Page 28: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/28.jpg)
PHP Code Sniffer
$ phpcs /path/to/code/myfile.php
FILE: /path/to/code/myfile.php------------------------------------------------------------------------FOUND 5 ERROR(S) AFFECTING 2 LINE(S)------------------------------------------------------------------------ 2 | ERROR | Missing file doc comment 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6------------------------------------------------------------------------
![Page 29: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/29.jpg)
PHP Code Sniffer
$ svn commit -m "Test" temp.phpSending temp.phpTransmitting file data .svn: Commit failed (details follow):svn: 'pre-commit' hook failed with error output:
FILE: temp.php--------------------------------------------------------------FOUND 1 ERROR(S) AND 0 WARNING(S) AFFECTING 1 LINE(S)-------------------------------------------------------------- 2 | ERROR | Missing file doc comment--------------------------------------------------------------
![Page 30: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/30.jpg)
This is important because things are standardized
![Page 31: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/31.jpg)
Measuring code complexityCyclomatic complexity
Directly measures the number of linearly independent paths through a program's source code.a.k.a. 1 + the number of times it branches
![Page 32: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/32.jpg)
public function render() { $code = null;
if (isset($this->headers['location']) && $this->status['code'] === 200) { $code = 302; }
if (!$status = $this->status($code)) { throw new Exception('Invalid status code'); } $this->_writeHeader($status);
foreach ($this->headers as $name => $value) { $key = strtolower($name);
if ($key == 'location') { $this->_writeHeader("Location: {$value}", $this->status['code']); } elseif ($key == 'download') { $this->_writeHeader('Content-Disposition: attachment; filename="' . $value . '"'); } elseif (is_array($value)) { $this->_writeHeader( array_map(function($v) use ($name) { return "{$name}: {$v}"; }, $value) ); } elseif (!is_numeric($name)) { $this->_writeHeader("{$name}: {$value}"); } } }
Measuring code complexity
![Page 33: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/33.jpg)
Measuring code coverage
![Page 34: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/34.jpg)
Measuring documentation coverageCheck it out @ http://thechaw.com/api_generator
![Page 35: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/35.jpg)
Measuring documentation coverageCheck it out:http://thechaw.com/api_generator
A series of rules
Assigns weights based on docblock content and various docblock tags
![Page 36: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/36.jpg)
Measuring documentation coverageBasic checks:
Do doc tags exist?Incomplete @param tags?Do @param tags match actual params?Does it have a @link to the man page?
![Page 37: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/37.jpg)
Profiling
![Page 38: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/38.jpg)
ProfilingGet timing / memory usage on every test run
Granular, get statistics per test method
Using continuous integration, code is profiled on each commit, all on a granular level
![Page 39: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/39.jpg)
Case study
![Page 40: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/40.jpg)
CakePHP 1.2 release cycle
1.2 alpha 1.2 beta 1.2 RC1 1.2 RC2 1.2 RC3 1.2 RC4
![Page 41: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/41.jpg)
Metrics are your canary in the coal mine of your development cycle
![Page 42: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/42.jpg)
By tracking profiler stats (and other metrics), we can see trends over time, and catch problems before they become problems
![Page 43: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/43.jpg)
Plus, who doesn’t like pretty graphs?
![Page 44: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/44.jpg)
Finding things to measure
Lithium Inspector classLithium Parser classBased on the awesome work of Sean Coateshttp://github.com/scoates/tokalizer
![Page 45: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/45.jpg)
Finding things to measure
![Page 46: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/46.jpg)
Finding things to measure
![Page 47: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/47.jpg)
Finding things to measure
![Page 48: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/48.jpg)
Finding things to measure
![Page 49: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/49.jpg)
Finding things to measure
![Page 50: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/50.jpg)
In a dynamic language like PHP, this is a hard problem.
![Page 51: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/51.jpg)
However, deep code introspection allows us to ask & answer some very interesting questions
![Page 52: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/52.jpg)
Project mess detection in PHPUnit
![Page 53: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/53.jpg)
Beyond copy-paste detection & into pattern recognition
![Page 54: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/54.jpg)
High-level refactoring tools
![Page 55: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/55.jpg)
Can “good code” be quantified??
![Page 56: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/56.jpg)
When is “good enough”... good enough?
![Page 57: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/57.jpg)
Technical debt
![Page 58: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/58.jpg)
Incentives and social coercion
![Page 59: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/59.jpg)
Choose your own adventure!
![Page 60: Measuring Your Code 2.0](https://reader033.vdocuments.net/reader033/viewer/2022052820/54b7a7cf4a7959b0218b45db/html5/thumbnails/60.jpg)
Go write better code