1

if/unless 寫作不要用多重否定句啊

 2 years ago
source link: https://blog.niclin.tw/2018/11/26/if/unless-%E5%AF%AB%E4%BD%9C%E4%B8%8D%E8%A6%81%E7%94%A8%E5%A4%9A%E9%87%8D%E5%90%A6%E5%AE%9A%E5%8F%A5%E5%95%8A/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nic Lin's Blog

喜歡在地上滾的工程師

多重否定句真的很煩啊!

雖然 ruby 有 unless 可以用,就可以不用寫 if not

不過可以用 if ,就避免用 unless ,除非 unless 較符合原意。

unless content.blank?
  # ....
end

畢竟 unless not 還是比較難在第一時間讀懂的,舉一個比較極端的例子 XD

unless !content.present?
  # ....
end

尤其兩個條件以上或有 else 時更不建議用 unless。

if !foo?
  # ....
else
  # ....
end
unless foo? && baz?
  # ....
end

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK