动机
将条件表达式高亮,这样的话你能清楚将其分开
对分叉之后的结果进行高亮
double disabilityAmount() {
if (_seniority < 2) return 0;
if (_monthsDisabled > 12) return 0;
if (_isPartTime) return 0;
// compute the disability amount
to
double disabilityAmount() {
if (isNotEligableForDisability()) return 0;
// compute the disability amount
在条件表达式的每个分支上有着相同的一片代码
将这段重复代搬移到条件表达式之外
if (isSpecialDeal()) {
total = price * 0.95;
send();
}
else {
total = price * 0.98;
send();
}
to
if (isSpecialDeal()) {
total = price * 0.95;
}
else {
total = price * 0.98;
}
send();
动机
使得变量清晰并保持相同
在一系列的布尔表达式中,某个变量带有“控制标记”的作用
已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;
}
}
}
}
to
void checkSecurity(String[] people) {
for (int i = 0; i < people.length; i++) {
if (people[i].equals ("Don")){
sendAlert();
break; // or return
}
if (people[i].equals ("John")){
sendAlert();
break; // or return
}
}
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-89441-17.html
应该说靠本本分分挣钱
我们国家可以把土地重复利用我们也可以用好多年的积蓄买一套二三线城市的房