84669 人が学習中
152542 人が学習中
20005 人が学習中
5487 人が学習中
7821 人が学習中
359900 人が学習中
3350 人が学習中
180660 人が学習中
48569 人が学習中
18603 人が学習中
40936 人が学習中
1549 人が学習中
1183 人が学習中
32909 人が学習中
大家平时代码是怎么写的,如何竟可能的减少if语句的嵌套,让代码更好维护
我一直提倡:
好的习惯例如异常情况等提前return掉
例如, 一般写法: function demo() { if(...) { //.... }else { // ... } return ... } 提前return写法: function demo() { if(...) { return ... } return ... }
其次,使用异常,使用try...catch...捕获,
class Demo { public function test() { if(...) { throw Exception('message', 'code'); } //code... } } try { //code... $demo = new Demo(); $demo->test(); }catch(Exception $e) { //code... }
我一直提倡:
好的习惯例如异常情况等提前return掉
其次,使用异常,使用try...catch...捕获,