Nonsense
More good code but also to post-maintenance, even if the latter does not require you to maintain, clear dry code will leave a good impression of the author
And can exert influence in the work of the team
Elimination using nested reverse logic
Business scene
When flying needs time before departure to receive boarding passes at the security check last weather matters flight case is to take a plane to take off
Time after takeoff time ----> Meal
Not receive boarding passes -----> receive boarding passes before take-off
No security ----> security check before take-off
Weather matters flight ---> flight
Weather should not be flying ---> Meal
Realization of the code waterfall
if(currentTime() < airplane.startTime() ){
//时间在有效范围内
if(boardingPass(user)){
//拥有登机牌
if(securityCheck(user)){
//安检完成
if(haveAGoodDay()){
return //起飞
}else{
return //改签
}
}else{
return //进行安检
}
}else{
return //领取登机牌
}
}else{
return //改签
}
看见这样的代码 不知道是否有兴趣在读下去
这样的代码给人一种逻辑不清晰的感觉 虽然逻辑上确实没问题
复制代码
Elegance of the code written package
The traffic information is encapsulated, the logic is not confused
The reverse logic code implemented
if(currentTime() > airplane.startTime()){
return //改签
}
if(!haveAGoodDay()){
return //改签
}
if(!boardingPass(user)){
return //领取登机牌
}
if(securityCheck(user)){
return //安检
}
return //起飞
用逆向思维消灭层层嵌套
代码中出现多次嵌套时:
1. 逻辑不清晰
2. 逻辑清晰但未将其视为一个业务整体,按照思维栈的思考编码
3. 多层嵌套容易出现问题
复制代码
to sum up
Reached under multiple or not the purpose and conditions of business may wish to try the next reverse logic
Vigilant thinking stack encoding logic, priority be considered as a whole
Multi-Nesting is the code in question