(1)Decompose Conditional(分解条件)
把大块头代码分解成函数
if(date.before(DATE_START) || data.after(DATE_END)){
charge = quantity * _winterCharge * windcharge;
} else {
charge = quantity * sumRage;
}
=》替换
private boolean noteSummber(){
return date.before(DATE_START) || data.after(DATE_END);
}
private double winterCharge(){
return quantity * _winterCharge * windcharge;
}
private double sumCharge(){
return quantity * sumRage;
}
if(noteSummber()){
charge = winterCharge();
} else {
charge = sumCharge();
}
(2)Consolidate Conditional Expression(合并条件式)
有的时候条件检查各不相同,而结果却一致,这时可以考虑用logic-AND 或者logic-OR将条件合并为一个表达式
double disabilityAmount(){
if(_seriv<0) return 0;
if(_mountDisabled<12) return 0;
if(_isPartTime) return 0;
//amount
...
}
=》替换 logic-OR
boolean isNotForDisabled (){
return ((_seriv<0) || (_mountDisabled<12) || _isPartTime);
}
double disabilityAmount(){
if(isNotForDisabled ()) return 0;
//amount
...
}
logic-AND 实例
if(overs()){
if(lengthOfService()>10){
return 0;
}
}
return 0.5;
等价
if(overs() && lengthOfService()>10) {
return 0;
} else {
return 0.5;
}
return (overs() && lengthOfService()>10)?0:0.5;
(3) Compsolidate Duplicate Conditional Fragments(合并重复的条件片段)
有时候,如果一组条件式的所有分支都执行了所有相同的某个代码,就应该把这段代码移动到条件以外
if(isSepcialDeal()){
total = price * 0.95;
send();
} else {
total = price * 0.98;
send();
}
=》替换
if(isSepcialDeal()){
total = price * 0.95;
} else {
total = price * 0.98;
}
send();
(4)Remove ControlFlag (移除控制标记)
以break continue return语句代替控制标记
void checkSecurity(String[] people){
for (int i = 0 ;i <people.length; i++){
boolean isFound = false;
if(!isFound){
if(people[i].equals("Don")){
sendAlert();
isFound = true;
}
if(people[i].equals("JOM")){
sendAlert();
isFound = true;
}
}
}
}
=》替换
public void checkSecurity(String[] people){
for (int i = 0 ;i <people.length; i++){
if(people[i].equals("Don")){
sendAlert();
break;
}
if(people[i].equals("JOM")){
sendAlert();
break;
}
}
}
return 语句返回控制标记
public void checkSecurity(String[] people){
String found = findScran(people);
somerAlert(found);
}
public String findScran(String[] people){
String found = "";
if("".equals(found)){
for (int i = 0 ;i <people.length; i++){
if(people[i].equals("Don")){
sendAlert();
found = "Don"
}
if(people[i].equals("JOM")){
sendAlert();
found = "JOM";
}
}
}
}
(5) Replace Nested Conditional with Guard clauses 以卫语句取代嵌套语句
double getPayAmount(){
double result;
if(_isCount) result = deadAmount();
else{
if(_separated) result = separatAmount();
else{
if(_isRetired) result = retiredAmount();
else{
result = nomalAmount();
}
}
}
return result;
}
=》替换
if(_isCount) reutrn deadAmount();
if(_separated) reutrn separatAmount();
if(_isRetired) reutrn retiredAmount()
reutrn nomalAmount();
条件式一般有两种,一种是所有分支都属于正常行为,另外一种是提供的答案只有一种是正常的,另外的都不正常。
通过替换我们可以看出嵌套的if else 讲究的是一个入口和一个出口,而替换后的函数代码更清晰,只要符合条件
了就直接返回退出,而不需要再看后面的代码。
特例:将条件反转
pubic double getCapital(){
double result = 0.0;
if(_capital>0.0){
if(_intRate > 0.0 && _duration >0.0){
result = (intRate/duration)*rate;
}
}
return result;
}
=》替换
public double getCapital(){
double result = 0.0;
if(_capital <=0) return result;
if(_intRate > 0.0 && _duration >0.0){
result = (intRate/duration)*rate;
}
return result;
}
