嗨,我是 Fly,用 Ruby 寫 Chatbot 並挑戰30天分享心得
為確保不會沒靈感
每日含 Ruby 主題文章增加內容
https://github.com/leo424y/clean-code-ruby
掌握錯誤
Bad:
require 'logger'
logger = Logger.new(STDOUT)
begin
function_that_might_throw()
rescue StandardError => err
logger.info(err)
end
Good:
require 'logger'
logger = Logger.new(STDOUT)
# Change the logger level to ERROR to output only logs with ERROR level and above
logger.level = Logger::ERROR
begin
function_that_might_throw()
rescue StandardError => err
# Option 1: Only log errors
logger.error(err)
# Option 2: Notify end-user via an interface
notify_user_of_error(err)
# Option 3: Report error to a third-party service like Honeybadger
report_error_to_service(err)
# OR do all three!
end
Bad:
def initialize(user)
fail unless user
...
end
Good:
def initialize(user)
fail ArgumentError, 'Missing user' unless user
...
end