yyqng

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

    代码应当用“自然语言”编写

1. 清楚地描述逻辑

 1 $is_admin = is_admin_request();
 2 if ($document) {
 3     if (!$is_admin && ($document['username'] != $_SESSION['username'])) {
 4         return not_authorized();
 5     }
 6 } else {
 7     if (!$is_admin) {
 8         return not_authorized();
 9     }
10 }
11 // continue rendering the page ...
12 
13 //自然语言的描述
14 //1. 你是管理员
15 //2. 你拥有该文档
16 //否则,无法授权
17 if (is_admin_request()) {
18     // authorized
19 } elseif ($document && ($document['username'] == $_SESSION['username'])) {
20     // authorized
21 } else {
22     return not_authorized();
23 }
24 // continue rendering the page ...

 

2. 了解函数库是有帮助的

 1 // 功能:在网站中循环显示下一个提示
 2 // 试想如果不用.next()库函数,代码肯定不会这么精简
 3 var show_next_tip = function () {
 4     var cur_tip = $('.tip:visible').hide();
 5     var next_tip = cur_tip.next('.tip');
 6     if (next_tip.size() === 0) {
 7         next_tip = $('.tip:first');
 8     }
 9     next_tip.show();
10 };

 

posted on 2021-01-16 16:35  zziii  阅读(209)  评论(0编辑  收藏  举报