Giter Club home page Giter Club logo

docs's Introduction

Hi there 👋

xn213's Github stats

Visiters

HitCount

docs's People

Contributors

xn2113 avatar xn213 avatar

Stargazers

 avatar

docs's Issues

代码整洁之道读书笔记

代码整洁之道读书笔记

“程序员必须为了让人能阅读代码而书写代码,而机器执行只是顺便的。” ----Harold Abelson

让代码比你来时干净

一、有意义的命名

  1. 变量、函数或者类的名称应该告诉你它为什么存在,它做什么事情,应该怎么用。
  2. 要名副其实,让人明白代码是做了什么事情
  3. 避免误导。如不是数组不要以list结尾,可以用group或者直接用复数形式。避免使用很相似的命名。
  4. 做有意义的区分。别同时用productData,productInfo。
  5. 用读的出来的名称,可搜索的名称。单字母名称仅用于短方法的本地变量。名称长短应与作用域大小相对应,变量如果可能在代码中多出使用,应赋予便于搜索的名称。
  6. 类名和对象名应该是名词,不该是动词。方法名应该是动词
  7. 每个概念对应一个词。类似于fetch、retrieve、get及controllers和managers只用其中的一个。
  8. 避免一个单词用于不同目的。
  9. 使用命名代替魔术数字。使用SECONDS_PER_DAY代替86400。

二、函数

  1. 短小。函数不应该长于一屏。
  2. 只做一件事情。
  3. 参数。少用多参数,如果有三个以上参数,可以将其封装为对象。参数的名字与函数的名字应该有良好的关系
  4. 无副作用。副作用就是做与名字无关的事情。
  5. 不重复。

三、注释

注释的恰当用法是弥补我们用代码表达意图时遭遇的失败。注释的更新不及时。只有代码才是真实的。

  1. 注释不能美化糟糕的代码。
  2. 用代码代替注释
  3. 好注释。提供信息的注释,解释意图的注释。TODO。警示。
  4. 坏注释。多余的注释、废弃的注释、糟糕的注释、被注释的代码。

四、格式

  1. 垂直格式。空白行分割代码,相关联的代码应该互相靠近。变量声明尽可能靠近使用位。函数调用者在被调用者的上面。
  2. 水平格式。遵循代码规范。

彻底搞清楚 instanceof

/**
 * instanceof 判断 left 是不是right类型的对象
 * @param { * } L
 * @param { * } R
 * @return { Boolean }
 */
function instanceof_2 ( L, R ) {
  let R_P = R.prototype
  let L = L.__proto__
  while( true ) {
    if( L === null || L === undefined){
      return false
    }
    if( L === R_P ) {
       return true
    }
    L = L.__proto__
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.