解决方法
我几乎从不使用then关键字.但是,有一种情况我认为它大大提高了可读性.请考虑以下多条件if语句.
例A
if customer.jobs.present? && customer.jobs.last.date.present? && (Date.today - customer.jobs.last.date) <= 90 puts 'Customer had a job recently' end
线长太长.难以阅读.
例B
if customer.jobs.present? && customer.jobs.last.date.present? && (Date.today - customer.jobs.last.date) <= 90 puts 'Customer had a job recently' end
条件何处结束以及内部代码从何处开始.根据必须Ruby样式指南,你有一个额外的缩进空间用于多行条件,但我仍然觉得它不容易阅读.
例C
if customer.jobs.present? && customer.jobs.last.date.present? && (Date.today - customer.jobs.last.date) <= 90 then puts 'Customer had a job recently' end
对我来说,示例C是目前最清楚的.而那就是利用当时的伎俩.