This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Updating the Bicycle Class | |
# 1 -------------------Okay | |
class Bicycle | |
attr_reader :size, :parts | |
def initialize(args={}) | |
@size = args[:size] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Writing the Concrete Code | |
# 1 -------------------Bad | |
class Schedule | |
def scheduled?(schedulable, start_date, end_date) | |
puts "This #{schedulable.class} " + | |
"is not scheduled\n" + | |
" between #{start_date} and #{end_date}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Starting Class | |
# 1 -------------------Okay | |
class Bicycle | |
attr_reader :size, :tape_color | |
def initialize(args) | |
@size = args[:size] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Overlooking Ducks | |
# 1 -------------------BAD | |
class Trip | |
attr_reader :bicycles, :customers, :vehicle | |
def prepare(mechanic) | |
# no depedency on mecahnic |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Law of Demeter | |
# set of coding rules that results in loosely coupled objects. It prohibits | |
# routing a message to a third object via a second object of a different type. | |
# Paraphrased: "only talk to your immediate neighbors", "use only one dot" | |
class Trip | |
def depart | |
# "train wrecks" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# Needs to Manage Dependencies | |
class Gear | |
attr_reader :chainring, :cog, :rim, :tire | |
def initialize(chainring, cog, rim, tire) | |
@chainring = chainring | |
@cog = cog | |
@rim = rim |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1 -------------------BAD | |
class Gear | |
attr_reader :chainring, :cog | |
def initialize(chainring, cog) | |
@chainring = chainring | |
@cog = cog | |
end | |
def ratio |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ###################### | |
# Refactored | |
# ###################### | |
# app/services/stock_service.rb | |
class StockService | |
def initialize(user) | |
@user = user | |
@stocks = [] | |
@stock_symbols = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ###################### | |
# Refactored | |
# ###################### | |
# app/controllers/stocks_controller.rb | |
require 'stock_quote' | |
require 'pp' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'stock_quote' | |
require 'pp' | |
class StocksController < ApplicationController | |
before_action :load_user | |
skip_before_filter :verify_authenticity_token | |
def index | |
update_stocks | |
end |
NewerOlder