嗨,我是 Fly,用 Ruby 寫 Chatbot 並挑戰30天分享心得
為確保不會沒靈感
每日含 Ruby 主題文章增加內容
https://github.com/leo424y/clean-code-ruby
推薦RuboCop
Bad:
DAYS_IN_WEEK = 7
daysInMonth = 30
songs = ['Back In Black', 'Stairway to Heaven', 'Hey Jude']
Artists = ['ACDC', 'Led Zeppelin', 'The Beatles']
def eraseDatabase; end
def restore_database; end
class ANIMAL; end
class Alpaca; end
Good:
DAYS_IN_WEEK = 7
DAYS_IN_MONTH = 30
SONGS = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'].freeze
ARTISTS = ['ACDC', 'Led Zeppelin', 'The Beatles'].freeze
def erase_database; end
def restore_database; end
class Animal; end
class Alpaca; end
Bad:
class PerformanceReview
def initialize(employee)
@employee = employee
end
def lookup_peers
db.lookup(@employee, 'peers')
end
def lookup_manager
db.lookup(@employee, 'manager')
end
def peer_reviews
peers = lookup_peers
# ...
end
def perf_review
peer_reviews
manager_review
self_review
end
def manager_review
manager = lookup_manager
# ...
end
def self_review
# ...
end
end
review = PerformanceReview.new(employee)
review.perf_review
Good:
class PerformanceReview
def initialize(employee)
@employee = employee
end
def perf_review
peer_reviews
manager_review
self_review
end
def peer_reviews
peers = lookup_peers
# ...
end
def lookup_peers
db.lookup(@employee, 'peers')
end
def manager_review
manager = lookup_manager
# ...
end
def lookup_manager
db.lookup(@employee, 'manager')
end
def self_review
# ...
end
end
review = PerformanceReview.new(employee)
review.perf_review