嗨,我是 Fly,用 Ruby 寫 Chatbot 並挑戰30天分享心得
為確保不會沒靈感
每日含 Ruby 主題文章增加內容
https://github.com/leo424y/clean-code-ruby
Bad:
def make_bank_account
# ...
{
balance: 0
# ...
}
end
account = make_bank_account
account[:balance] = 100
account[:balance] # => 100
Good:
class BankAccount
def initialize
# this one is private
@balance = 0
end
# a "getter" via a public instance method
def balance
# do some logging
@balance
end
# a "setter" via a public instance method
def balance=(amount)
# do some logging
# do some validation
@balance = amount
end
end
account = BankAccount.new
account.balance = 100
account.balance # => 100
Good:
class Toy
attr_accessor :price
end
toy = Toy.new
toy.price = 50
toy.price # => 50