Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Fighting Code Smells #FrozenRails
Search
Dennis Ushakov
September 21, 2012
Technology
0
59
Fighting Code Smells #FrozenRails
Ruby code quality tool overview
Dennis Ushakov
September 21, 2012
Tweet
Share
More Decks by Dennis Ushakov
See All by Dennis Ushakov
Ruby Debugger Internals
denofevil
1
70
RubyMine and RubyMotion
denofevil
0
50
Ruby Debugger API
denofevil
0
130
RubyMotion #JetBrainsDay
denofevil
0
42
Get More from RubyMotion with RubyMine
denofevil
0
49
Other Decks in Technology
See All in Technology
Claude_CodeでSEOを最適化する_AI_Ops_Community_Vol.2__マーケティングx_AIはここまで進化した.pdf
riku_423
2
570
小さく始めるBCP ― 多プロダクト環境で始める最初の一歩
kekke_n
1
420
AI駆動PjMの理想像 と現在地 -実践例を添えて-
masahiro_okamura
1
110
Data Hubグループ 紹介資料
sansan33
PRO
0
2.7k
CDKで始めるTypeScript開発のススメ
tsukuboshi
1
430
GitHub Issue Templates + Coding Agentで簡単みんなでIaC/Easy IaC for Everyone with GitHub Issue Templates + Coding Agent
aeonpeople
1
230
今日から始めるAmazon Bedrock AgentCore
har1101
4
410
Codex 5.3 と Opus 4.6 にコーポレートサイトを作らせてみた / Codex 5.3 vs Opus 4.6
ama_ch
0
150
Oracle Cloud Observability and Management Platform - OCI 運用監視サービス概要 -
oracle4engineer
PRO
2
14k
Ruby版 JSXのRuxが気になる
sansantech
PRO
0
150
Red Hat OpenStack Services on OpenShift
tamemiya
0
110
学生・新卒・ジュニアから目指すSRE
hiroyaonoe
2
620
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
117
110k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.6k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.4k
Everyday Curiosity
cassininazir
0
130
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
320
My Coaching Mixtape
mlcsv
0
48
We Are The Robots
honzajavorek
0
160
Optimizing for Happiness
mojombo
379
71k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
190
Building the Perfect Custom Keyboard
takai
2
680
A Modern Web Designer's Workflow
chriscoyier
698
190k
Odyssey Design
rkendrick25
PRO
1
500
Transcript
Fighting Code Smells ! ruby code analysis tools Dennis Ushakov
Pareto Principle • 20% Write new code •80% Modify existing
code
Does This Smell? a = something.valid?! ! if (a.to_s ==
"true")! # code here! end!
Does This Smell? def has_currency_rate?! val = false! if currency.id
== company.currency.id or rate.blank?! else! val = true! end! val! end!
Does This Smell? class FinancialEventObserver < ActiveRecord::Observer! observe Payment, Invoice!
def before_save(model)! event = nil ! if model.class == Payment! if model.new_record? ! event = FinancialEvent.new(:event => FinancialEvent::Event::PAYMENT_INVOICE,! :arguments => {:client_name => model.invoice.client.short_name, :invoice_number => model.invoice.invoice_number},! :company_id=>model.invoice.client.company.id)! end! elsif model.class == Invoice! i = Invoice.find_by_id model.id ! if model.new_record? or i.status != model.status ! if model.status == Invoice::Status::ESTIMATE! event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_SEND,! :arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},! :company_id=>model.client.company.id)! elsif model.status == Invoice::Status::APPROVED! event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_APPROVED,! :arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},! :company_id=>model.client.company.id)! elsif model.status == Invoice::Status::REJECTED! event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_REJECTED,! :arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},! :company_id=>model.client.company.id)! elsif model.status == Invoice::Status::SEND! event = FinancialEvent.new(:event => FinancialEvent::Event::INVOICE_SEND,! :arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},! :company_id=>model.client.company.id) ! end! elsif !model.new_record? and i.state != model.state! if model.state == Invoice::State::DELETED! event = FinancialEvent.new(:event => FinancialEvent::Event::INVOICE_DELETED,! :arguments => {:invoice_number => model.invoice_number},! :company_id=>model.client.company.id)! end! end ! end! event.eventable = model.requester unless event.blank? ! event.save unless event.blank? !
Code That Smells • Runtime errors • Runtime warnings •
Dead code • Copy/paste • Complex method bodies • Code style violations • Framework pattern violations
Code Quality Tools autotest cucumber rspec simplecov rcov reek flay
dust pelusa roodi flog metrics_fu heckle Static Runtime
Static Tools • Inspect your code without launching it •
100% side effects free • Challenging to implement • Rails DSL magic kills ‘em
Reek • Class, module, method, identifier names • Using is_a?,
kind_of? instead of duck typing • Code duplicates • Large classes and methods • Nested iterators
Flog • Uses ABC metrics system • Assignment • Branches
• Conditions • |ABC| = √(A²+B²+C²) • Consider revising most painful methods
Flay • Analyzes code duplicates • Ignores identifier/ constants names
• Consider refactoring code that’s reported
Roodi • Assignments in conditions • Case blocks without else
• Large classes, modules and methods • Naming conventions • Cyclomatic complexity
metrics_fu/metrical Provides aggregate results for • Flay • Flog •
Reek • Roodi
Runtime Tools • Inspect your code by launching it •
100% follow the way Ruby works • Cope well with DSL magic • May have side effects • Works until the very first failure
Runtime Tools • Code testing • Test::Unit/MiniTest • RSpec/Cucumber •
Autotest/CI • Code coverage • RCov/SimpleCov • Heckle
Heckle • Changes code • if → unless • Calls
are changed • Numbers are changed • Runs tests • At least one test should fail after change
RubyMine • Static analysis • On-the-fly inspections with quickfixes •
Understands Rails DSLs • Code duplication • Dynamic analysis tools integration • Graphical representation • Autotest simulation • Stacktrace navigation
Refactorings • Rename • Extract • Method • Class/Module •
Variable/Parameter
Morale • Use static analysis tools • Don’t forget to
test • Try RubyMine http://jetbrains.com/ ruby
en_Dal
[email protected]
denofevil