Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Solid foundations for Rails apps

Solid foundations for Rails apps

A talk given at Railsberry, the European Rails Conference, on Apr 20, 2012.

Mariusz Lusiak

April 22, 2012
Tweet

More Decks by Mariusz Lusiak

Other Decks in Programming

Transcript

  1. OOP

  2. ISP SRP DRY DI Liskov Open/Closed Least surprise Tell Don’t

    Ask Law of Demeter Convention over Configuration Domain Language Capture
  3. ISP SRP DRY DI Liskov Open/Closed Least surprise Tell Don’t

    Ask Law of Demeter Convention over Configuration Domain Language Capture
  4. SRP

  5. class User attr_reader :name end class BandwidthUsage def initialize(user) user

    = @user end def by_month(month) # Calculate bandwidth usage end end
  6. class Shipment def update_destination(name, address) @first_name, @last_name = name.split(' ')

    @address = address end def update_last_name(name) @last_name = name.split(' ').last end end
  7. class Shipment def update_destination(name, address) @first_name, @last_name = extract_names(name) @address

    = address end def update_last_name(name) @last_name = extract_names(name).last end private def extract_names(name) name.split(' ') end end
  8. class Shipment def update_destination(name, address) name_parser = NameParser.new(name) @first_name =

    name_parser.first_name @last_name = name_parser.last_name @address = address end def update_last_name(name) name_parser = NameParser.new(name) @last_name = name_parser.last_name end end
  9. class Post < ActiveRecord::Base include FigLeaf hide ActiveRecord::Base, ancestors: true,

    except: [Object, :init_with, :new_record?, :errors, :valid?, :save] hide_singletons ActiveRecord::Calculations, ActiveRecord::FinderMethods, # ...
  10. Is it easier to add a method to an existing

    class or create a new class with that method?
  11. DRY