your rails code better
play

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


  1. Using MetricFu to Make Your Rails Code Better Jake Scruggs Senior Consultant @ Obtiva

  2. Boring who am I stuff • High School Physics teacher • Apprenticed at Object Mentor • 6 projects at ThoughtWorks • Now a consultant at Obtiva

  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 on every project • It’s since become much more

  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)

  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

  6. Bang zoom, you’ve got some metrics

  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

  8. MetricFu uses a bunch of open source projects to give all this to you • Rcov • Reek • Flog • Roodi • Saikuro • Flay • Rails ‘stats’ • Churn

  9. Code Coverage: So easy you’re probably already doing it

  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

  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

  12. Complexity Measurement Saikuro Flog

  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

  14. Flog score example: the numbers don’t add up

  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

  16. Flog is Opinionated • Documentation is lacking • But its relative scores are good • More importantly, Flog knows where the badness lives in Ruby

  17. Saikuro Example: Explain that Saikuro measures cyclomatic complexity Hey, where’s the dynamic one? Explain Cyclomatic Complexity Saikuro uses a simplifed CC calulation -- just closed loops and not paths

  18. How does Flog do?

  19. So now what? • Create a ‘hit list’ of your most complex methods • Examine the worst offenders • Refactor, refactor, refactor

  20. Damerau Levenshtein Distance Example

  21. Refactoring complex methods yields many benefits • Smaller, easier to understand methods • Finding bugs • You can see past the craziness and into the code

  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”

  23. Consider these methods:

  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

  25. Rails Stats (rake stats)

  26. Reek (code smells - may not be bad)

  27. Roodi (more selective about reporting)

  28. Flay

  29. Source Control Churn

  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

  31. All this gets put into a yaml file

  32. So you can consume or mash it up as you like

  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)

  34. Continuous Code Metrics I highly recommend using CruiseControl.rb or Integrity to set up a metrics build.

  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

  36. What Code Metrics can do for you • Help you prioritize • Shine light on unknown problems • bugs • hidden complexity • Provide another perspective

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend