object oriented design in the - amazon...
TRANSCRIPT
@jessitron
Object Oriented Design in the
6 package-level principles +
DRY YAGNI+&
SOLID
classestypesJava
Haskell Ruby
ECMAScript
?
Single Responsibility
Principle
SOLID
Don’tRepeatYourself
Sell
Track
Buy
class Good implements InvItem, Purchase, Sellable{ InventoryImpl inventory; PurchaseImpl purchase; SellableImpl sellable; ... }
Java
Ruby
class Good include SellableItem include InventoryTracking include Purchaseend
Ruby
joy discipline
Haskell
joydiscipline
data Good = Good SaleInfo InvInfo PurchInfo
instance ReceiptItem Good where format Good s _ _ = desc s + price s
Haskell
dictionaries
functions
ECMAScript
ECMAScript
var good = _.extend({}, invInfo, saleInfo, purInfo)
{ name: “Canteen”, sku: 37671287, price: 1399, format: function() {...} cost: 715}
InterfaceSegregation
Principle
SOLID
String receiptLine(Sellable item)
Java
ECMAScript
function receiptLine(item) { console.log(“Cha-ching! profit ” + item.price - item.cost) return format(item.desc, item.price) }
Haskell
print :: ReceiptItem -> String
LiskovSubstitution
Principle
SOLID
Java
List sneaky = new MyList()...
if (sneaky.length == -1) { System.out.println(“WAT!”)}
LSP
PLS
Principle of
Least
Surprise
Ruby
def form_receipt_line(item_sold) ... line_with_desc_and_priceend
violate expectations
access global state
modify input
change the world
DependencyInversionPrinciple
SOLID
App
Hibernate
App
HibernatePersistence
API
HibernatePersistence APIAppfunction
another function
data
ECMAScript
find items
format receipt
lineitem data
var items = _.map(codes, findItem)var lines = _.map(items, formatReceiptLine)
> cat data.csv | grep 42 | cut -f2 | sort
ksh
Open/ClosedPrinciple
SOLID
Extension
Closed
App
Module
LibraryClosed for
modification
> cat data.csv | grep 42 | cut -f2 | sort
package principles
3 x cohesion3 x coupling
couplingcohesion
Common Reuse
Principle
Common ChangesPrinciple
Reuse-ReleaseEquivalency
Principle
App
LibraryReuse This
right problem
best solutionevidence of actual use
name and location
code reviews documentation
testsexisting functionality
maintenanceburden
machinery
bugslegacy code
Planning for reuse is premature
optimization
Premature optimizationis the root of all evil
Donald Knuth
AcyclicDependencies
Principle
App
Module
Library
App
Module
Library
Module Module
Library LibraryLibraryLibrary
App
Stable Abstractions
Principle
Stable Dependency
Principle
Increasing stability
Concrete
Abstract
Reusable
You Are Here
Abstract
DRY
getTaxRate capitalize
customerRewards formatDate
YouAin’tGonnaNeed It
ConcreteYou Are Here Simple
Time Reading
UseChange
$ $$$
6 package-level principles +
DRY YAGNI+&
SOLID
SmallClear intent
older = better
@jessitronFreedom
@jessitron@jessitron