アーリーリターン

リーダブルコードに書かれているが、アーリーリターンの美味しさは下記。

  • メソッドの途中でリターンすることで読むコード数を減らせる

    7.5節 関数から早く返す

  • ネストを浅くできる

    7.7節 ネストを浅くする

指摘を受けた部分は下記のところ。

def run_wc(file_path: nil, sentence: nil, l_option: false)
  if sentence.nil?
    "wc: #{file_path}: open: No such file or directory"
  else
    concat_wc_contents(file_path, l_option, sentence)
  end
end

メソッド自体は短いため、早く返すのはそれほどだけど、if文がなくなりネストを浅くできました😃

def run_wc(file_path: nil, sentence: nil, l_option: false)
  return "wc: #{file_path}: open: No such file or directory" if sentence.nil?y

  concat_wc_contents(file_path, l_option, sentence)
end