YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: Measuring Your Code 2.0

Measuring your code

@nateabele

[email protected]

A talk by Nate Abele

Page 2: Measuring Your Code 2.0

Feedback

http://joind.in/1601

Page 3: Measuring Your Code 2.0

Confession

Page 4: Measuring Your Code 2.0
Page 5: Measuring Your Code 2.0

Coercion, red bars & broken windows

Page 6: Measuring Your Code 2.0

Continuous

Page 7: Measuring Your Code 2.0

Goal: Perfection

Page 8: Measuring Your Code 2.0

Or at the least, continuous improvement

Page 10: Measuring Your Code 2.0

What people usually think

Estimating costs

Projecting deadlines

Managerial BS!

Page 11: Measuring Your Code 2.0

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

...and my personal favorite

Social network

Page 13: Measuring Your Code 2.0

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

Wherefore... (WTF) ?

Page 15: Measuring Your Code 2.0

“Engineer” & “architect”

Page 16: Measuring Your Code 2.0
Page 17: Measuring Your Code 2.0

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

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

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

What can we measure?

Page 21: Measuring Your Code 2.0

Code!!

Page 22: Measuring Your Code 2.0

More specifically...Unit test coverage

Complexity

Speed

Documentation

Page 23: Measuring Your Code 2.0

More specifically...

Standards conformance

Refactoration!

Page 24: Measuring Your Code 2.0

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

Notes on continuous integrationA build system

Runs on every code commit

Runs tests

Reports

Page 26: Measuring Your Code 2.0

Metric examples

Page 27: Measuring Your Code 2.0

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

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

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

This is important because things are standardized

Page 31: Measuring Your Code 2.0

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

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

Measuring code coverage

Page 34: Measuring Your Code 2.0

Measuring documentation coverageCheck it out @ http://thechaw.com/api_generator

Page 35: Measuring Your Code 2.0

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

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

Profiling

Page 38: Measuring Your Code 2.0

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

Case study

Page 40: Measuring Your Code 2.0

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

Metrics are your canary in the coal mine of your development cycle

Page 42: Measuring Your Code 2.0

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

Plus, who doesn’t like pretty graphs?

Page 44: Measuring Your Code 2.0

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

Finding things to measure

Page 46: Measuring Your Code 2.0

Finding things to measure

Page 47: Measuring Your Code 2.0

Finding things to measure

Page 48: Measuring Your Code 2.0

Finding things to measure

Page 49: Measuring Your Code 2.0

Finding things to measure

Page 50: Measuring Your Code 2.0

In a dynamic language like PHP, this is a hard problem.

Page 51: Measuring Your Code 2.0

However, deep code introspection allows us to ask & answer some very interesting questions

Page 52: Measuring Your Code 2.0

Project mess detection in PHPUnit

Page 53: Measuring Your Code 2.0

Beyond copy-paste detection & into pattern recognition

Page 54: Measuring Your Code 2.0

High-level refactoring tools

Page 55: Measuring Your Code 2.0

Can “good code” be quantified??

Page 56: Measuring Your Code 2.0

When is “good enough”... good enough?

Page 57: Measuring Your Code 2.0

Technical debt

Page 58: Measuring Your Code 2.0

Incentives and social coercion

Page 59: Measuring Your Code 2.0

Choose your own adventure!

Page 60: Measuring Your Code 2.0

Go write better code


Related Documents