Clean Code
Clean Code
Intention revealing
Pronounceable
Distinctable
Class names should be nouns / noun verbs
Method names should be verbs / verb
phrase
One Word per concept
end_date_time
sdt
start_date_time
nme
name
fname
stotal
VS
full_name
subtotal
pgup
page_up
lng
longitud
description
destroy
delete
name
fullname
place
house
process
VS
location
home
execute
Noun
User
Form
Friend
Noun phrase
Signup
DiscountCalculator
TripFinder
Verb
invite
authorize
validate
Verb Phrase
find_location
has_friendship_with
send_email
Functions should...
class Order
# Good. One level of abstraction, One thing
# Remember to declare utility functions just
# below where they are being called
def total
subtotal - discount
end
def subtotal
items.sum(:price)
end
def discount
items.sum(:discount)
end
end
Not OK
Ok!
class Payment
attr_reader payment_method
def process
case payment_method.class
when "CreditCart"
when "Deposit"
when "Cash"
end
end
Not OK
Ok!
Comments are...
Shitz
A pain in the ass to maintain
To be avoided if we follow good design
practices
Not OK
Ok!
Classes should...
# By dividing responsibilities
# we ended up with more objects
class User
attr_accessor first_name, last_name, email
def full_name
"#{first_name} #{last_name}"
end
end
class PasswordRetriever
def retrieve
end
end
class Signup
def sign_up
end
private
def send_confirmation_email
end
end
Not OK
Ok!
def select
# codez, codez...
end
end
Not OK
Ok!
Not OK
class jockey
# use only one dot!
def run_horse
horse.run
end
end
Ok!