Your Rails Code Better Jake Scruggs Senior Consultant @ Obtiva - - PowerPoint PPT Presentation

your rails code better
SMART_READER_LITE
LIVE PREVIEW

Your Rails Code Better Jake Scruggs Senior Consultant @ Obtiva - - PowerPoint PPT Presentation

Using MetricFu to Make Your Rails Code Better Jake Scruggs Senior Consultant @ Obtiva Boring who am I stuff High School Physics teacher Apprenticed at Object Mentor 6 projects at ThoughtWorks Now a consultant at Obtiva The hell


slide-1
SLIDE 1

Using MetricFu to Make Your Rails Code Better

Jake Scruggs Senior Consultant @ Obtiva

slide-2
SLIDE 2

Boring who am I stuff

  • High School Physics teacher
  • Apprenticed at Object Mentor
  • 6 projects at ThoughtWorks
  • Now a consultant at Obtiva
slide-3
SLIDE 3

The hell is a metric_fu?

  • It’s a mash-up of various code analysis tools
  • I got tired of re-writing the same rake tasks
  • n every project
  • It’s since become much more
slide-4
SLIDE 4

How do I use it?

Run the following if you haven't already: gem sources -a http://gems.github.com Install the gem(s): sudo gem install jscruggs-metric_fu (You may crash the wifi if you do this now)

slide-5
SLIDE 5

Then what?

  • require ‘metric_fu’ in your Rakefile
  • Or you could vendor it because you’re a

good person

  • And then: rake metrics:all
slide-6
SLIDE 6

Bang zoom, you’ve got some metrics

slide-7
SLIDE 7

Why do this?

  • Code rots one day at a time
  • George Carlin’s Law:

“Everyone slower than me is stupid, and everyone faster is crazy”

  • Prioritize your efforts
slide-8
SLIDE 8

MetricFu uses a bunch

  • f open source projects

to give all this to you

  • Rcov
  • Flog
  • Saikuro
  • Rails ‘stats’
  • Reek
  • Roodi
  • Flay
  • Churn
slide-9
SLIDE 9

Code Coverage:

So easy you’re probably already doing it

slide-10
SLIDE 10

What’s a good coverage number?

  • With Ruby, 100% is very possible
  • 30% is bad, but 100% may not be good
  • But are my tests any good?
  • A good goal is as many tests as there are

paths

slide-11
SLIDE 11

Sadly

  • Code coverage means something when it’s

low, but may mean nothing when it’s high

  • Still, it has its uses
  • C1 or C2 coverage would be cool

C0 is did you hit the line -- Rcov measures this C1 deals with partially executed lines: if foo() and bar() #foo() could return false and bar() would not execute C2 deals with all the possible paths through a method: see rcov.html

slide-12
SLIDE 12

Complexity Measurement

Saikuro Flog

slide-13
SLIDE 13

Managing complexity is just as important as TDD

  • If you had to choose:
  • Spaghetti code with spaghetti tests
  • Well factored code with no tests

Yes, properly done TDD should produce good design. However, people seem to forget about the refactor part of Red, Green, Refactor

slide-14
SLIDE 14

Flog score example:

the numbers don’t add up

slide-15
SLIDE 15

General guidelines for flog scores per method

  • 0-10 Awesome
  • 11-20 Good enough
  • 21-40 Might need refactoring
  • 41-60 Possible to justify
  • 61-100 Danger
  • 100-200 Whoop, whoop, whoop
  • 200 + Someone please think of the children
slide-16
SLIDE 16

Flog is Opinionated

  • Documentation is lacking
  • But its relative scores are good
  • More importantly, Flog knows where the

badness lives in Ruby

slide-17
SLIDE 17

Saikuro Example:

Hey, where’s the dynamic one?

Explain that Saikuro measures cyclomatic complexity Explain Cyclomatic Complexity Saikuro uses a simplifed CC calulation -- just closed loops and not paths

slide-18
SLIDE 18

How does Flog do?

slide-19
SLIDE 19

So now what?

  • Create a ‘hit list’ of your most complex

methods

  • Examine the worst offenders
  • Refactor, refactor, refactor
slide-20
SLIDE 20

Damerau Levenshtein Distance Example

slide-21
SLIDE 21

Refactoring complex methods yields many benefits

  • Smaller, easier to understand methods
  • Finding bugs
  • You can see past the craziness and into the code
slide-22
SLIDE 22

Why inject sucks

  • Keep in mind that new developers will be

joining your team

  • Remember Carlin’s Law: “Everyone slower

than me is stupid, and everyone faster is crazy”

slide-23
SLIDE 23

Consider these methods:

slide-24
SLIDE 24

Explain Yourself

  • If you really believe the complexity is a net

gain then keep it, but explain

  • framework code
  • complex problem space
  • Test it well -- more complexity means

more tests (one per path is a good goal)

  • Ex: ‘Data’ Serialization
slide-25
SLIDE 25

Rails Stats (rake stats)

slide-26
SLIDE 26

Reek

(code smells - may not be bad)

slide-27
SLIDE 27

Roodi

(more selective about reporting)

slide-28
SLIDE 28

Flay

slide-29
SLIDE 29

Source Control Churn

slide-30
SLIDE 30

Look for Outliers with Churn

  • It may be a ‘god’ object
  • It may also be a view that changes a lot
  • Try to let metric_fu help you challenge

your assumptions

slide-31
SLIDE 31

All this gets put into a yaml file

slide-32
SLIDE 32

So you can consume or mash it up as you like

slide-33
SLIDE 33

Creating a new metric_fu metric

  • Ex. I want to analyze git logs to find out

people’s check in habits

  • Inherit from Generator.
  • Implement: Emit, analyze, and to_h methods
  • Write a template to display the hash

(Show an example)

slide-34
SLIDE 34

Continuous Code Metrics

I highly recommend using CruiseControl.rb or Integrity to set up a metrics build.

slide-35
SLIDE 35

Metrics: the downside

  • Numbers do lie
  • Any analysis tool has an opinion
  • ‘Bad’ Numbers may be good
  • Managers and code metrics:
  • If they’re not in the code they can’t make

judgment calls

  • Gaming the system
slide-36
SLIDE 36

What Code Metrics can do for you

  • Help you prioritize
  • Shine light on unknown problems
  • bugs
  • hidden complexity
  • Provide another perspective
slide-37
SLIDE 37

Metrics are not a ‘Fire and Forget’ operation If your code is not getting better every day then it’s getting worse

slide-38
SLIDE 38

Thanks

Me Jake Scruggs Blog http://jakescruggs.blogspot.com Home Page http://metric-fu.rubyforge.org Group http://groups.google.com/group/metric_fu GitHub http://github.com/jscruggs/metric_fu