Measuring Your Code
-
Upload
nate-abele -
Category
Technology
-
view
2.340 -
download
4
description
Transcript of Measuring Your Code
Measuring your code
@nateabele
A talk by Nate Abele
Confession
Coercion, red bars & broken windows
Continuous
Goal: Perfection
Or at the least, continuous improvement
a.k.a. 改善 (kaizen)
What people usually think
Estimating costs
Projecting deadlines
Managerial BS!
Client Spec SheetFlash intro with no load time
User account logins, password optional
Ajax chat
“Like Google”
(actual bullet points, some paraphrased)
...and my personal favorite
Social network
Measurement is an essential element of management; there is little chance of controlling what we can not measure.
- Wikipedia, “Software metric”
Wherefore... (WTF) ?
“Engineer” & “architect”
Cognitive DissonanceEngineers deal with tangible, immutable constraints, like gravity
The practice of developing software is an inherently creative discipline
*
* Thank you, Jones
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
/
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
What can we measure?
Code!!
More specifically...Unit test coverage
Complexity
Speed
Documentation
More specifically...
Standards conformance
Refactoration!
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)
Notes on continuous integrationA build system
Runs on every code commit
Runs tests
Reports
Metric examples
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”
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------------------------------------------------------------------------
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--------------------------------------------------------------
This is important because things are standardized
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
Measuring code complexity
Measuring code coverage
Measuring documentation coverageCheck it out @ http://thechaw.com/api_generator
Measuring documentation coverageCheck it out:http://thechaw.com/api_generator
A series of rules
Assigns weights based on docblock content and various docblock tags
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?
Profiling
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
Case study
CakePHP 1.2 release cycle
1.2 alpha 1.2 beta 1.2 RC1 1.2 RC2 1.2 RC3 1.2 RC4
Metrics are your canary in the coal mine of your development cycle
By tracking profiler stats (and other metrics), we can see trends over time, and catch problems before they become problems
Plus, who doesn’t like pretty graphs?
Finding things to measure
CakePHP Inspector classCakePHP Parser classBased on the awesome work of Sean Coateshttp://github.com/scoates/tokalizer
Finding things to measure
Finding things to measure
Finding things to measure
Finding things to measure
Finding things to measure
In a dynamic language like PHP, this is a hard problem.
However, deep code introspection allows us to ask & answer some very interesting questions
Project mess detection in PHPUnit
Beyond copy-paste detection & into pattern recognition
High-level refactoring tools
Can “good code” be quantified??
Goodvs.
good enoughvs.
the economy