54
Measuring your code @nateabele [email protected] A talk by Nate Abele

Measuring Your Code

Embed Size (px)

DESCRIPTION

Examines some of the fundamental problems with the way the industry thinks about software "engineering", and breaks some notions in order to find useful ways of improving your code quality, and your skills and discipline as a developer.

Citation preview

Page 1: Measuring Your Code

Measuring your code

@nateabele

[email protected]

A talk by Nate Abele

Page 2: Measuring Your Code

Confession

Page 3: Measuring Your Code

Coercion, red bars & broken windows

Page 4: Measuring Your Code

Continuous

Page 5: Measuring Your Code

Goal: Perfection

Page 6: Measuring Your Code

Or at the least, continuous improvement

Page 8: Measuring Your Code

What people usually think

Estimating costs

Projecting deadlines

Managerial BS!

Page 9: Measuring Your Code

Client Spec SheetFlash intro with no load time

User account logins, password optional

Ajax chat

“Like Google”

(actual bullet points, some paraphrased)

Page 10: Measuring Your Code

...and my personal favorite

Social network

Page 11: Measuring Your Code

Measurement is an essential element of management; there is little chance of controlling what we can not measure.

- Wikipedia, “Software metric”

Page 12: Measuring Your Code

Wherefore... (WTF) ?

Page 13: Measuring Your Code

“Engineer” & “architect”

Page 14: Measuring Your Code
Page 15: Measuring Your Code

Cognitive DissonanceEngineers deal with tangible, immutable constraints, like gravity

The practice of developing software is an inherently creative discipline

*

* Thank you, Jones

Page 16: Measuring Your Code

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 17: Measuring Your Code

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 18: Measuring Your Code

What can we measure?

Page 19: Measuring Your Code

Code!!

Page 20: Measuring Your Code

More specifically...Unit test coverage

Complexity

Speed

Documentation

Page 21: Measuring Your Code

More specifically...

Standards conformance

Refactoration!

Page 22: Measuring Your Code

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 23: Measuring Your Code

Notes on continuous integrationA build system

Runs on every code commit

Runs tests

Reports

Page 24: Measuring Your Code

Metric examples

Page 25: Measuring Your Code

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 26: Measuring Your Code

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 27: Measuring Your Code

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 28: Measuring Your Code

This is important because things are standardized

Page 29: Measuring Your Code

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 30: Measuring Your Code

Measuring code complexity

Page 31: Measuring Your Code

Measuring code coverage

Page 32: Measuring Your Code

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

Page 33: Measuring Your Code

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 34: Measuring Your Code

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 35: Measuring Your Code

Profiling

Page 36: Measuring Your Code

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 37: Measuring Your Code

Case study

Page 38: Measuring Your Code

CakePHP 1.2 release cycle

1.2 alpha 1.2 beta 1.2 RC1 1.2 RC2 1.2 RC3 1.2 RC4

Page 39: Measuring Your Code

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

Page 40: Measuring Your Code

By tracking profiler stats (and other metrics), we can see trends over time, and catch problems before they become problems

Page 41: Measuring Your Code

Plus, who doesn’t like pretty graphs?

Page 42: Measuring Your Code

Finding things to measure

CakePHP Inspector classCakePHP Parser classBased on the awesome work of Sean Coateshttp://github.com/scoates/tokalizer

Page 43: Measuring Your Code

Finding things to measure

Page 44: Measuring Your Code

Finding things to measure

Page 45: Measuring Your Code

Finding things to measure

Page 46: Measuring Your Code

Finding things to measure

Page 47: Measuring Your Code

Finding things to measure

Page 48: Measuring Your Code

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

Page 49: Measuring Your Code

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

Page 50: Measuring Your Code

Project mess detection in PHPUnit

Page 51: Measuring Your Code

Beyond copy-paste detection & into pattern recognition

Page 52: Measuring Your Code

High-level refactoring tools

Page 53: Measuring Your Code

Can “good code” be quantified??

Page 54: Measuring Your Code

Goodvs.

good enoughvs.

the economy