Improving code design with automated checks

A
Improving Code Designwith
Automated Checks
AndyWaite
Engineering ManageratFinanceit
financeit.io/careers
1
RuboCop
Categories of rules:
» Linting
» Style
» Security
» Naming
» Metrics
@andyw8 2
ASimple PullRequest
class Product
...
+
+ def discounted_price
+ price * 0.5
+ end
end
tests are passing
@andyw8 3
RuboCopFailures
Offenses:
app/models/product.rb:1:1:
C: Metrics/ClassLength:
Class has too many lines. [101/100]
class Product < ApplicationRecord ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@andyw8 4
Configuration
# rubocop.yml
Metrics/ClassLength:
Enabled: false
@andyw8 5
6
Code Smells
» Large Class
» Long Function
» Long Parameter List
@andyw8 7
Possible Refactorings
For a Large Class:
» Extract Class
» Replace Type Code With Subclass
@andyw8 8
RuboCopMetrics
» Metrics/ClassLength
» Large Class (default: 100 lines)
» Metrics/MethodLength
» Long Function (default: 10 lines)
» Metrics/ParameterLists (default: 5 params)
» Long Parameter List
@andyw8 9
RuboCopSays...
"Hmm, this class is getting quite large. I wonder if
we should refactor it?"
@andyw8 10
AutomatingtheWorkflow
» Aim is to run RuboCop against each pull request
» Tools: Code Climate, HoundCI, Pronto
» Review the warnings
» Choose to act or ignore
@andyw8 11
Summary
» Code Metrics can give valuable insights
» Make them part of your automated checks
» Use them to detect potential Code Smells
» Don't make changes only to satisfy RuboCop
» But refactor when it makes sense to you
» Continually evolve your application's design
@andyw8 12
Thanks
GitHub/Twitter: @andyw8
awaite@financeit.io
13
1 of 13

More Related Content

Similar to Improving code design with automated checks(20)

Code reviewCode review
Code review
Abhishek Sur5K views
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
Max Kleiner259 views
Application Architecture April 2014Application Architecture April 2014
Application Architecture April 2014
Lars-Erik Kindblad3.2K views
Pragmatic Code CoveragePragmatic Code Coverage
Pragmatic Code Coverage
Alexandre (Shura) Iline1.6K views
Best practices in enterprise applicationsBest practices in enterprise applications
Best practices in enterprise applications
Chandra Sekhar Saripaka3.1K views
MDE in PracticeMDE in Practice
MDE in Practice
Abdalmassih Yakeen417 views
Clean Code V2Clean Code V2
Clean Code V2
Jean Carlo Machado250 views
NDepend Public PPT (2008)NDepend Public PPT (2008)
NDepend Public PPT (2008)
NDepend290 views
Code MetricsCode Metrics
Code Metrics
Attila Bertók87 views
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation792 views
Application ArchitectureApplication Architecture
Application Architecture
Lars-Erik Kindblad4.6K views

Improving code design with automated checks