Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
127 slides
-
1. Looks like you cared 2. Pretty much what you
expected 3. Language looks as perfect fit for problem -
1. Looks like you cared 2. Pretty much what you
expected 3. Language looks as perfect fit for problem My definition -
class Report def initialize @title = 'Monthly Report' @text =
[ 'Things are going', 'really, really well.' ] end ! def output_report ...HTML specific stuff... end end template method: reports -
bad code class Report def initialize @title = 'Monthly Report'
@text = [ 'Things are going', 'really, really well.' ] end ! def output_report ...HTML specific stuff... end end template method: reports -
template method: reports class Report def initialize @title = 'Monthly
Report' @text = ['Things are going', 'really, really well.'] end ! def output_report output_start output_head @text.each do |line| output_line(line) end output_end end ! def output_start; end ! def output_head output_line(@title) end ! def output_line(line) raise 'Called abstract method: output_line' end ! def output_end; end end -
template method: reports better code class Report def initialize @title
= 'Monthly Report' @text = ['Things are going', 'really, really well.'] end ! def output_report output_start output_head @text.each do |line| output_line(line) end output_end end ! def output_start; end ! def output_head output_line(@title) end ! def output_line(line) raise 'Called abstract method: output_line' end ! def output_end; end end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
class TokenizedModel < SimpleDelegator! def save! __getobj__.token ||= SecureRandom.urlsafe_base64! __getobj__.save!
end! ! def to_param! __getobj__.token! end! end TokenizedModel.new(invitation) Decorator Pattern -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def deliver! Mailer.invitation_notification(self).deliver! end! end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
class Invitation < ActiveRecord::Base! EMAIL_REGEX = /\A([^@\s]+)@((:?[a-z0-9]+\.)+[a-z]{2,})\z/i! STATUSES = %w(pending
accepted)! ! belongs_to :sender, class_name: 'User'! belongs_to :survey! ! before_create :set_token! ! validates :recipient_email, presence: true, format: EMAIL_REGEX! validates :status, inclusion: { in: STATUSES }! ! def to_param! token! end! ! def deliver! Mailer.invitation_notification(self).deliver! end! ! private! ! def set_token! self.token = SecureRandom.urlsafe_base64! end! end -
# does not follow OCP! class Purchase! def charge_user!! Stripe.charge(user:
user, amount: amount)! end! end -
# does not follow OCP! class Purchase! def charge_user!! Stripe.charge(user:
user, amount: amount)! end! end # follows OCP by depending on an abstraction! class Purchase! def charge_user!(payment_processor)! payment_processor.charge(user: user, amount: amount)! end! end -
class Printer! def initialize(item)! @item = item! end! ! def
print! thing_to_print = case @item! when Text! @item.to_s! when Image! @item.filename! when Document! @item.formatted! end! ! send_to_printer(thing_to_print)! end! end -
class Printer! def initialize(item)! @item = item! end! ! def
print! send_to_printer(@item.printable_representation)! end! end Dependency Injection -
# does not follow OCP! class Unsubscriber! def unsubscribe!! SubscriptionCanceller.new(user).process!
CancellationNotifier.new(user).notify! CampfireNotifier.announce_sad_news(user)! end! end -
class UnsubscriptionCompositeObserver! def initialize(observers)! @observers = observers! end! ! def
notify(user)! @observers.each do |observer|! observer.notify(user)! end! end! end! ! class Unsubscriber! def initialize(observer)! @observer = observer! end! ! def unsubscribe!(user)! observer.notify(user)! end! end Composite Pattern -
class UnsubscriptionCompositeObserver! def initialize(observers)! @observers = observers! end! ! def
notify(user)! @observers.each do |observer|! observer.notify(user)! end! end! end! ! class Unsubscriber! def initialize(observer)! @observer = observer! end! ! def unsubscribe!(user)! observer.notify(user)! end! end Observer Pattern -
class UnsubscriptionCompositeObserver! def initialize(observers)! @observers = observers! end! ! def
notify(user)! @observers.each do |observer|! observer.notify(user)! end! end! end! ! class Unsubscriber! def initialize(observer)! @observer = observer! end! ! def unsubscribe!(user)! observer.notify(user)! end! end -
Let q(x) be a property provable about objects x of
type T. Then q(y) should be provable for objects y of type S where S is a subtype of T. “ -
blah q(x) blah blah blah blah blah blah blah blah
x blah T. Blah q(y) blah blah blah blah blah blah y blah blah S blah S blah blah blah blah T. “ -
class Signup! def initialize(attributes)! @attributes = attributes! end! ! def
save! account = Account.create!(@attributes[:account])! account.users.create!(@attributes)! end! end! ! class SignupsController < ApplicationController! def create! build_signup.save! end! ! def build_signup! Signup.new(params[:signup])! end! end -
class InvitationSignup < Signup! def save(invitation)! user = super! invitation.accept(user)!
user! end! end A wild requirement appears… -
class SignupsController < ApplicationController! def create! signup = build_signup! if
params[:invitation_id]! invitation = Invitation.find(params[:invitation_id])! signup.save(invitation)! else! signup.save! end! end! ! def build_signup! if params[:invitation_id]! InvitationSignup.new(params[:signup])! else! Signup.new(params[:signup])! end! end! end -
class SignupsController < ApplicationController! def create! signup = build_signup! if
params[:invitation_id]! invitation = Invitation.find(params[:invitation_id])! signup.save(invitation)! else! signup.save! end! end! ! def build_signup! if params[:invitation_id]! InvitationSignup.new(params[:signup])! else! Signup.new(params[:signup])! end! end! end -
def InvitationSignup < Signup! def initialize(attributes, invitation)! super(attributes)! @invitation =
invitation! end! ! def save! user = super! @invitation.accept(user)! user! end! end The public API remains the same -
class SignupsController < ApplicationController! def create! build_signup.save! end! ! def
build_signup! if params[:invitation_id]! InvitationSignup.new(params[:signup])! else! Signup.new(params[:signup])! end! end! end -
public class WebPage implements IWebPage {! public String currentUrl() {};!
public void setUrl(String url) {};! public String[] consoleMessages() {};! public String[] alertMessages() {};! public String[] confirmMessages() {};! public String[] promptMessages() {};! } -
public class WebPage implements IWebPage {! public String currentUrl() {};!
public void setUrl(String url) {};! public String[] consoleMessages() {};! public String[] alertMessages() {};! public String[] confirmMessages() {};! public String[] promptMessages() {};! } Not all methods are related -
public class VisitCommand {! public VisitCommand(WebPage page, String url) {!
this.page = page;! this.url = url;! }! ! public void start() {! page.setUrl(url)! }! ! private WebPage page;! private String url;! } Command Pattern -
public interface HasUrl {! public String currentUrl() {};! public void
setUrl(String url) {};! }! ! public interface HasMessages {! public String[] consoleMessages() {};! public String[] alertMessages() {};! public String[] confirmMessages() {};! public String[] promptMessages() {};! } public class WebPage implements HasUrl, HasMessages { -
public class VisitCommand {! public VisitCommand(HasUrl page, String url) {!
this.page = page;! this.url = url;! }! ! public void start() {! page.setUrl(url)! }! ! private HasUrl page;! private String url;! } -
public class VisitCommand {! public VisitCommand(HasUrl page, String url) {!
this.page = page;! this.url = url;! }! ! public void start() {! page.setUrl(url)! }! ! private HasUrl page;! private String url;! } -
http://tenderlovemaking.com/2014/06/02/yagni-methods-are-killing-me.html loop do params[:foo] params.delete :foo params[:foo] = [Object.new] end
def convert_value_to_parameters(value) if value.is_a?(Array) && !converted_arrays.member?(value) converted = value.map { |_| convert_value_to_parameters(_) } converted_arrays << converted converted elsif value.is_a?(Parameters) || !value.is_a?(Hash) value else self.class.new(value) end end -
“High level modules should not depend on low level modules,
they should both depend on abstractions -
#Violates DIP! class Copier! def self.copy! reader = KeyboardReader.new! writer
= Printer.new! ! keystrokes = reader.read_until_eof! writer.write(keystrokes)! end! end -
#Obeys DIP. Is decoupled and reusable.! class Copier! def initialize(reader,
writer)! @reader = reader! @writer = writer! end! ! def copy! keystrokes = @reader.read_until_eof! @writer.write(keystrokes)! end! end! ! Copier.new(KeyboardReader.new, Printer.new)! Copier.new(KeyboardReader.new, NetworkPrinter.new) -
class NetworkPrinterAdapter! def initialize(network_printer)! @network_printer = network_printer! end! ! def
write(keystrokes)! @network_printer.print(keystrokes)! end! end! ! network_printer = ! NetworkPrinterAdapter.new(NetworkPrinter.new)! Copier.new(KeyboardReader.new, network_printer) Adapter Pattern -
class Product < ActiveRecord::Base! has_many :purchases! end! ! class Purchase
< ActiveRecord::Base! belongs_to :Product! ! def has_receipt?! receipt.present?! end! ! def receipt_id! receipt.id! end! ! def receipt_address! receipt.details.address! end! ! private! ! def receipt! Stripe::Receipt.find(stripe_order_id)! end! end -
class PurchaseWithReceipt < SimpleDelegator! def initialize(purchase, receipt_finder)! super(purchase)! @receipt_finder =
receipt_finder! end! ! def has_receipt?! receipt.present?! end! ! def receipt_id! receipt.id! end! ! def receipt_address! receipt.details.address! end! ! private! ! def receipt! @receipt_finder.find(stripe_order_id)! end! end class Product < ActiveRecord::Base! has_many :purchases! end! ! class Purchase < ActiveRecord::Base! belongs_to :product! end -
S O L I D ingle responsibility pen closed iskov
substitution nterface segragation ependency inversion Uncle Bob Martin -
Can’t tell what it does at a glance, it’s TOO
LONG More than one level of nesting, probably -
Can’t tell what it does at a glance, it’s TOO
LONG More than one level of nesting, probably TOO LONG -
Can’t tell what it does at a glance, it’s TOO
LONG More than one level of nesting, probably TOO LONG More than one level of abstraction, may be -
Can’t tell what it does at a glance, it’s TOO
LONG More than one level of nesting, probably TOO LONG TOO LONG More than one level of abstraction, may be -
Can’t tell what it does at a glance, it’s TOO
LONG More than one level of nesting, probably TOO LONG TOO LONG More than one level of abstraction, may be More than 5 lines -
Can’t tell what it does at a glance, it’s TOO
LONG More than one level of nesting, probably TOO LONG TOO LONG More than one level of abstraction, may be More than 5 lines TOO LONG -
Can’t tell what it does at a glance, it’s TOO
LONG More than one level of nesting, probably TOO LONG TOO LONG More than one level of abstraction, may be More than 5 lines TOO LONG REFACTOR -
Need to scroll to know what it does, it’s TOO
BIG More private methods than public, it’s -
Need to scroll to know what it does, it’s TOO
BIG More private methods than public, it’s TOO BIG -
Need to scroll to know what it does, it’s TOO
BIG More private methods than public, it’s TOO BIG More than seven methods, probably -
Need to scroll to know what it does, it’s TOO
BIG More private methods than public, it’s TOO BIG TOO BIG More than seven methods, probably -
Need to scroll to know what it does, it’s TOO
BIG More private methods than public, it’s TOO BIG TOO BIG More than seven methods, probably More than 100 lines -
Need to scroll to know what it does, it’s TOO
BIG More private methods than public, it’s TOO BIG TOO BIG More than seven methods, probably More than 100 lines TOO BIG -
Need to scroll to know what it does, it’s TOO
BIG More private methods than public, it’s TOO BIG TOO BIG More than seven methods, probably More than 100 lines TOO BIG REFACTOR -
Extract decorator: If the class has many objects related to
a single action Solution #2 -
class Report def initialize @title = 'Monthly Report' @text =
[ 'Things are going', 'really, really well.' ] end ! def output_report ...HTML specific stuff... end end strategy pattern: reports -
bad code class Report def initialize @title = 'Monthly Report'
@text = [ 'Things are going', 'really, really well.' ] end ! def output_report ...HTML specific stuff... end end strategy pattern: reports -
class Report def initialize(formatter) @title = 'Monthly Report' @text =
[ 'Things are going', 'really, really well.' ] @formatter = formatter.new(@title, @text) end ! def formatter=(formatter) @formatter = formatter.new(@title, @text) end ! def output_report @formatter.output_report end end strategy pattern: reports -
class Report def initialize(formatter) @title = 'Monthly Report' @text =
[ 'Things are going', 'really, really well.' ] @formatter = formatter.new(@title, @text) end ! def formatter=(formatter) @formatter = formatter.new(@title, @text) end ! def output_report @formatter.output_report end end better code strategy pattern: reports -
strategy pattern: reports report = Report.new(HTMLFormatter) report.output_report ! report.formatter =
PlainTextFormatter report.output_report -
Smelling feature envy: Local variables or params that are used
more than instance variables and methods -
Smelling feature envy: Private methods on the same class with
the same parameter -
Smelling feature envy: class Completion! def score! answers.inject(0) do |result,
answer|! question = answer.question! result + question.score(answer.text)! end! end! end -
Extract method: If possible move part of a method to
the appropriate class Solution #1 -
Solving feature envy: class Answer! def score! question.score(text)! end! end!
! class Completion! answers.inject(0) do |result, answer|! result + answer.score! end! end -
Hexagonal Architecture Domain Driven Design (DDD) Presenters Service Objects Value
Objects and Immutability Behavior Driven Development (BDD) Object Composition vs Inheritance