zoukankan      html  css  js  c++  java
  • 疯狂Java学习笔记(77)-----------凝视注意事项

    代码凝视,能够说是比代码本身更重要。这里有一些方法能够确保你写在代码中的凝视是友好的:

    不要反复阅读者已经知道的内容

    能明白说明代码是做什么的凝视对我们是没有帮助的。

    // If the color is red, turn it green
    if (color.is_red()) {
      color.turn_green();
    }

    要凝视说明推理和历史

    假设代码中的业务逻辑以后可能须要更新或更改,那就应该留下凝视:)

    /* The API currently returns an array of items
    even though that will change in an upcoming ticket.
    Therefore, be sure to change the loop style here so that
    we properly iterate over an object */
    
    var api_result = {items: ["one", "two"]},
        items = api_result.items,
        num_items = items.length;
    
    for(var x = 0; x < num_items; x++) {
      ...
    }

    同一行的凝视不要写得非常长

    没什么比拖动水平滚动栏来阅读凝视更令开发者发指的了。其实,大多数开发者都会选择忽略这类凝视。由于读起来真的非常不方便。

    function Person(name) {
      this.name = name;
      this.first_name = name.split(" ")[0]; // This is just a shot in the dark here. If we can extract the first name, let's do it
    }

    要把长凝视放在逻辑上面,短凝视放在后面

    凝视假设不超过120个字符那能够放在代码旁边。否则,就应该直接把凝视放到语句上面。

    if (person.age < 21) {
      person.can_drink = false; // 21 drinking age
    
      /* Fees are given to those under 25, but only in
         some states. */
      person.has_car_rental_fee = function(state) {
        if (state === "MI") {
          return true;
        }
      };
    }

    不要为了凝视而加入不必要的凝视

    画蛇添足的凝视会造成混乱。或许在学校里老师教你要给全部语句加入凝视,这会帮助开发者更好地理解。但这是错的。

    谁要这么说,那你就立刻上给他个两大耳刮子。

    代码应该保持干净简洁,这是毋庸置疑的。假设你的代码须要逐行解释说明,那么你最须要做的是重构。

    if (person.age >= 21) {
      person.can_drink = true; // A person can drink at 21
      person.can_smoke = true; // A person can smoke at 18
      person.can_wed = true; // A person can get married at 18
      person.can_see_all_movies = true; // A person can see all movies at 17
      //I hate babies and children and all things pure because I comment too much
    }

    凝视要拼写正确

    不要为代码凝视中的拼写错误找借口。IDE能够为你检查拼写。

    假设没有这个功能,那就去下载插件,自己动手!

    要多多练习

    熟能生巧。试着写一些实用的凝视。能够问问其它开发者你的凝视是否实用。随着时间的推移。你会慢慢懂得如何才算是友好的凝视。

    要审查别人的凝视

    在代码审查时,我们往往会忽略查看凝视。不要怕要求很多其它的凝视。你应该提出质疑。假设每一个人都养成写好凝视的好习惯,那么世界将会更美好。

    总结

    凝视是开发进程中很重要的一部分,但我们不应该为了凝视而凝视。凝视应该是实用的。简洁的,应该是对代码的一种补充。

    凝视不应该用于逐行地解释代码,相反,它应该用于解释业务逻辑,推理以及对将来的启发。

    转自http://www.codeceo.com/article/comments-do-and-dont.html

  • 相关阅读:
    iOS开发WKWebView Cookie的读取与写入,与UIWebView的Cookie共享
    【拨号】iPhone拨号功能隐藏代码,值得收藏。
    iOS: 获取不变的UDID
    ios 系统设置对应URL
    iOS开发UI篇—多控制器和导航控制器简单介绍
    iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(二)
    iOS开发UI篇—Date Picker和UITool Bar控件简单介绍
    iOS开发UI篇—程序启动原理和UIApplication
    iOS开发UI篇—常见的项目文件介绍
    iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(一)
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7212642.html
Copyright © 2011-2022 走看看