当前位置: 移动技术网 > IT编程>软件设计>架构 > 学习重构(5)-简化条件表达式

学习重构(5)-简化条件表达式

2019年12月08日  | 移动技术网IT编程  | 我要评论
1.Decompose Conditional (分解条件表达式)应用场景:你有一个复杂的条件(if-then-else)语句。从if、then、else三个段落中分别提炼出独立函数。示例:if (date.before(SUMMER_START) || date.after(SUMMER_END) ...

1.decompose conditional (分解条件表达式)
应用场景:你有一个复杂的条件(if-then-else)语句。从if、then、else三个段落中分别提炼出独立函数。
示例:
if (date.before(summer_start) || date.after(summer_end)) {
  charge = quantity * mwinterrate + mwinterservicecharge;
} else {
  charge = quantity * msummerrate;
}
重构为:
if (notsummer(date)) {
  charge = wintercharge(quantity);
} else {
  charge = summercharge(quantity);
}
private boolean notsummer(date date) {
  return date.before(summer_start) || date.after(summer_end);
}
private double wintercharge(int quantity) {
  return quantity * mwinterrate + mwinterservicecharge;
}
private double summercharge(int quantity) {
  return quantity * msummerrate;
}

2.consolidate conditional expression (合并条件表达式)
应用场景:你有一系列条件测试,都得到相同结果。将这些测试合并为一个条件表达式,并将这个条件表达式提炼成一个独立函数。
示例:
double disabilityamount() {
if(mseniority < 2) {
return 0;
}
if(mmonthsdisabled > 12) {
return 0;
}
if(misparttime) {
return 0;
}
// compute the disability amount ...
}
重构为:
double disabilityamount() {
  if(isnoteligiblefordisability()) {
    return 0;
  }
  // compute the disability amount ...
}
private boolean isnoteligiblefordisability() {
  return (mseniority < 2) || (mmonthsdisabled > 12) || misparttime;
}

3.consolodate duplicate conditional fragments (合并重复的条件片段)
应用场景:在条件表达式的每个分支上有着相同的一段代码。将这段重复代码搬移到条件表达式之外。
示例:
if(isspecialdeal()) {
  total = price * 0.95; send();
} else {
  total = price * 0.98; send();
}
重构为:
if(isspecialdeal()) {
  total = price * 0.95;
} else {
  total = price * 0.98;
}
send();

4.remove control flag (移除控制标记)
应用场景:在一系列布尔表达式中,某个变量带有“控制标记”的作用。以break语句或return语句取代控制标记。
示例:void checksecurity(string[] people) {
  boolean found = false;
  for(int i = 0; i < people.length; i++) {
    if(!found) {
      if(people[i].equals("don")) {
      sendalert();
      found = true;
    }
    if(people[i].equals("john")) {
      sendalert();
      found = true;
} } } }
重构为:void checksecurity(string[] people) {
  for(int i = 0; i < people.length; i++) {
    if(people[i].equals("don")) {
    sendalert();
    break;
  }
  if(people[i].equals("john")) {
    sendalert();
    break;
} } }

5.replace nested conditional with guard clauses (以卫语句取代嵌套条件表达式)
应用场景:函数中的条件逻辑使人难以看清正常的执行路径。使用卫语句表现所有特殊情况。
条件表达式常有两种表现形式:a)所有分支都属于正常行为;b)表达分支中只有一种是正常行为,其他都是不常见的情况。如果两条分支都是正常行为,就应该使用形如if...else...的条件表达式;如果某个条件极其罕见,就应该单独检查该条件,并在该条件为真时立刻从函数中返回。这样的单独检查常被称为“卫语句”。
示例:
double getpayamount() {
  double result; if(misdead) {
    result = deadamount();
  } else {
    if(misseparated) {
      result = separatedamount();
    } else {
      if(misretired) {
        result = retiredamount();
      } else {
        result = normalpayamount();
} } }
return result;
}

重构为:
double getpayamount() {
  if(misdead) {
    return deadamount();
  }
  if(misseparated) {
    return separatedamount();
  }
  if(misretired) {
    return retiredamount();
  }
  return normalpayamount();
}

6.replace conditional with polymorphism (以多态取代条件表达式)
应用场景:你手上有个条件表达式,它根据对象类型的不同而选择不同的行为。将这个条件表达式的每个分支放进一个子类内的覆写函数中,然后将原始函数声明为抽象函数。
示例:
double getspeed() {
  switch(mtype) {
    case european: return getbasespeed();
    case african: return getbasespeed() - getloadfactor() * mnumberofcoconuts;
    case norwegian_blue: return misnailed ? 0 : getbasespeed(mvoltage);
  }
  throw new runtimeexception("should be unreachable.");
}

重构为:
abstract class bird {
  abstract double getspeed();
}

class european extends bird {
  double getspeed() {
  return getbasespeed();
} }

class african extends bird() {
  double getspeed() {
  return getbasespeed() - getloadfactor() * mnumberofcoconuts;
} }

class norwegianblue extends bird {
  double getspeed() {
  return misnailed ? 0 : getbasespeed(mvoltage);
} }

7. introduce null object (引入null对象)
应用场景:你需要再三检查某对象是否为null,将null值替换为null对象。
示例:
if (custom == null) {
plan = billingplan.basic();
} else {
plan = custom.getplan();
}
重构为:
class custom {
public plan getplan() {
return normalplan;
}

public boolean isnull() {
return false;
}
}

class nullcustom extends custom {
public plan getplan() {
return billingplan.basic();
}

public boolean isnull() {
return true;
}
}

8. introduce assertion (引入断言)
应用场景:某一段代码需要对程序状态做出某种假设。以断言明确表现这种假设。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网