zoukankan      html  css  js  c++  java
  • 《编写可读代码的艺术》第12章 把想法变成代码

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

    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 };
  • 相关阅读:
    ubuntu12.04启动系统时报错
    TCP&HTTP协议详解
    nginx日志分析、切割与防盗链
    Nginx Rewrite规则详解
    nginx location深入剖析
    hadoop自动安装脚本
    极易中文分词
    朴素贝叶斯算法分析及java 实现
    随机森林(Random Forest)
    ubuntu 13.04 安装 JDK
  • 原文地址:https://www.cnblogs.com/yyqng/p/14286464.html
Copyright © 2011-2022 走看看