LoginSignup
0
0

More than 1 year has passed since last update.

【Ruby】「A && B && C」のリファクタリング

Last updated at Posted at 2021-12-09

でこんなリファクタリングの仕方ができると知ったので、
Rubyで書き換えてみた。

JS

before

if (card == "valid" && tries <= 3 && balance <= 3000) {
    withdrawMoney();
}

after

function canGetMoney() {
    if (card != "valid") return false;
    if (tries > 3) return false;
    if (balance > 3000) return false;
    return true;
}
if (canGetMoney()) {
    withdrawMoney();
}

Ruby

before

if card == "valid" && tries <= 3 && balance <= 3000
   withdraw_money
end

after

def can_get_money
  return false if card != "valid"
  return false if tries > 3
  return false if balance > 3000
  true
end

withdraw_money if can_get_money
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0