Slide 1

Slide 1 text

Saturday, June 15, 13

Slide 2

Slide 2 text

Ivan the Terrible’s Blog A Ruby on Rails Anti-patterns Example Saturday, June 15, 13

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Gang of Four (GOF) Saturday, June 15, 13

Slide 6

Slide 6 text

• 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

Slide 7

Slide 7 text

• OOP • SRP • Managing Dependencies • Creating Flexible Interfaces • Duck Typing • Inheritance • Modules • Composition • Testing Saturday, June 15, 13

Slide 8

Slide 8 text

• 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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Code Quality Gems • SimpleCov • rails_best_practices • reek • seattle.rb: flog, flay, and heckle • roodi • cane Saturday, June 15, 13

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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