Rails Performance Basics: Ivan the Terrible's Blog

Rails Performance Basics: Ivan the Terrible's Blog

Topics on Ruby Code Quality and Rails Performance

Ea2273b4310db9ac0109dfaefd489a06?s=128

Ivan Storck

August 20, 2013
Tweet

Transcript

  1. Saturday, June 15, 13

  2. Ivan the Terrible’s Blog A Ruby on Rails Anti-patterns Example

    Saturday, June 15, 13
  3. What are AntiPatterns? • There must be at least two

    key elements present to formally distinguish an actual AntiPattern from a simple bad habit, bad practice, or bad idea: • A repeated pattern of action, process, or structure that initially appears to be beneficial but ultimately produces more bad consequences than beneficial results • A refactored solution that is clearly documented, proven in actual practice, and repeatable • AntiPatterns: Refactoring Software, Architectures, and Projects in Crisis (William Brown, Raphael Malveau, Skip McCormick, and Tom Mowbray) Saturday, June 15, 13
  4. What are you reading? • It might be asked of

    you in an interview • It’s a great go-to for “Do you have any questions?” Saturday, June 15, 13
  5. Gang of Four (GOF) Saturday, June 15, 13

  6. • Separate Out the Things That Change from Those That

    Stay the Same • Program to an Interface, Not an Implementation • Prefer Composition over Inheritance • Delegate, Delegate, Delegate • YAGNI • and many more - specific to ruby Saturday, June 15, 13
  7. • OOP • SRP • Managing Dependencies • Creating Flexible

    Interfaces • Duck Typing • Inheritance • Modules • Composition • Testing Saturday, June 15, 13
  8. • Voyeuristic Models • Fat Models • Spaghetti SQL •

    Duplicate Code Duplication • Authorization Astronaut • The Million Model March • PHPitis • Markup Mayhem • Homemade Keys • Fat Controller • Bloated Sessions • Monolithic Controllers • Controller of Many Faces • A Lost Child Controller • Rat’s Nest Resources • and many more Saturday, June 15, 13
  9. AntiPattern: Voyeuristic Models <%= @invoice.customer.name %> <%= @invoice.customer.address.street %> <%=

    @invoice.customer.address.city %> <%= @invoice.customer.address.state %> <%= @invoice.customer.address.zip_code %> class Address < ActiveRecord::Base belongs_to :customer end class Customer < ActiveRecord::Base has_one :address has_many :invoices end class Invoice < ActiveRecord::Base belongs_to :customer end Saturday, June 15, 13
  10. AntiPattern: Voyeuristic Models Problem: Zero Encapsulation • Encapsulation: 1/4 core

    OOP concepts. • The other three are inheritance, polymorphism, and abstraction. • makes class members (variables and methods) private, and only allows access thorough public methods. • a wall protecting your data from being modified, or accessed in ways that you, the programmer, may not want. In properly encapsulated code data inside a class is only accessible through these public interfaces. • Benefit: ability to modify our code without breaking other parts of our program, makes our programs much easier to maintain, extend, and gives you much more flexibility. Saturday, June 15, 13
  11. AntiPattern: Voyeuristic Models Solution: Delegation, Law of Demeter class Address

    < ActiveRecord::Base belongs_to :customer end class Customer < ActiveRecord::Base has_one :address has_many :invoices delegate :street, :city, :state, :zip_code, :to => :address end class Invoice < ActiveRecord::Base belongs_to :customer delegate :name, :street, :city, :state, :zip_code, :to => :customer, :prefix => true end Saturday, June 15, 13
  12. AntiPattern: Voyeuristic Models Solution: Delegation, Law of Demeter <%= @invoice.customer.name

    %> <%= @invoice.customer.address.street %> <%= @invoice.customer.address.city %> <%= @invoice.customer.address.state %> <%= @invoice.customer.address.zip_code %> <%= @invoice.customer_name %> <%= @invoice.customer_street %> <%= @invoice.customer_city %> <%= @invoice.customer_state %> <%= @invoice.customer_zip_code %> Saturday, June 15, 13
  13. AntiPattern: Voyeuristic Models Solution: Delegation, Law of Demeter <%= @invoice.name

    %> <%= @invoice.street %> <%= @invoice.city %> <%= @invoice.state %> <%= @invoice.zip_code %> class Invoice < ActiveRecord::Base belongs_to :customer delegate :name, :street, :city, :state, :zip_code, :to => :customer, :prefix => false end Saturday, June 15, 13
  14. Code Quality Gems • SimpleCov • rails_best_practices • reek •

    seattle.rb: flog, flay, and heckle • roodi • cane Saturday, June 15, 13
  15. Assignment / Practice • Fix this shit • Measurement before

    and after. Timing and Code Quality • Pagination • Caching • Styling • Awards for various metrics (Speed, Quality, Style) Saturday, June 15, 13
  16. The blog code git clone https://github.com/ivanoats/ivan_the_terribles_blog bundle rake db:migrate rake

    load_blog:load_shit rails s Saturday, June 15, 13