Isolating Boundaries
Andrei Lisnic (@andrei_lisnic)
We are programmers
we translate ideas into code
Idea ↔ code
because we can think on different levels
of abstraction
Leaky abstractions
A well-designed system
Has well defined boundaries between its levels of abstraction
Single responsibility
a object should have only one reason to change
Single responsibility
class Email
def get_content
end
...
def draw
end
end
Single responsibility
good bad
class Email
def get_content
end
...
def draw
end
end
class Email
def get_content
end
...
end
class EmailView
def draw
end
end
Constants
class UserRepository
def all
JSON.parse RestClient.get(“http://somehost/api/users/”)
end
#...other methods that use http://somehost/api/users/
end
Constants
class UserRepository
USERS_URL = “http://somehost/api/users/”
def all
JSON.parse RestClient.get(USERS_URL)
end
end
class UserRepository
USERS_URL = “http://somehost/api/users/”
def all
parse(get(USERS_URL))
end
...
private
def parse response
JSON.parse response
end
def get url
RestClient.get
end
end
Functional data manipulation
def users_with_not_recommended_reports
hospital.reports.created_today
.with_score_bigger(0.5)
.not_aproved
.map(&:user)
.uniq
end
- Don’t copy/paste code, build simple interfaces
- Don’t find and replace, give names to things
Thank you