-
- 我们希望我们的开发人员和审阅者考虑如何编写漂亮的代码。 它与研究一样重要,因为出色的研究工作需要落地和应用才能使他人受益。 此外,出色的工程能力也有助于我们的职业发展。
- 研究是科学,而工程更像艺术。 优秀的软件开发工程师应该从可用代码发展为健壮,可靠和宽容的代码。 推荐阅读:为什么你的前端工作经验不值钱?
-
配置
- 我们使用了ESLint 和 Prettier来统一代码风格,保证代码质量,但是我们希望开发者能够理解一些值得注意的规则。
- 仍有一些规则不在我们的 linter 范围内,我们希望开发者和维护者都能努力执行这些规则。我们从Google JavaScript Style Guide中总结了一些规则如下。
-
文件名必须小写,可以包含虚线(
-),但不要乱加其他标点符号(除非特殊需要) -
文件必须包括一个文件信息,包括:作者,描述,用途,文件依赖,示例:
/** * @fileoverview Utilities for handling textareas. * Provide a series of functions. * Based on d3.js, lodash.js. * @author Ran Chen <crcrcry.hello@gmail.com> */
-
在
README.md中提供组件组织结构。示例:components/ |- todolist/ |- index.vue |- todolist-item.vue |- todolist-item-button.vue |- index.html |- README.md |- search-bar/ |- index.vue |- search-bar-navigation.vue |- search-bar-button.vue |- index.html |- README.md
- 引入模块时,需要有模块的扩展名;
- 注意:当前的 TypeScript (3.9.3) 不支持引入带拓展名的 ts 文件,所以引入
.ts文件时不需要加拓展名。
- 命名引入的模块
- 使用
import * as name时,name应该用首字母小写的驼峰规则(lowerCamelCase)对文件名进行重写。 - 引入
default模块时,命名应直接源自原模块,并且应该根据对象类型使命名符合[命名规则](# 4 命名)。 - 引入带名字的模块时,应该保持原名。重命名的话需要最好打上警告。
- 使用
- 不要循环引用
示例:
import * as fileOne from '../file-one.js'
import MyClass from '../my-class.js'
import SimpleComponent from '../simple-component.vue'
import SearchBar from '../search-bar'
import { addTwoNumbers, mySqrt } from '../my-math.js'
import { SOME_CONSTANT } from '../constant.js'-
变量:为结构复杂的变量加上注释;
-
对象:
- 对象的方法最好能够使用 shorthand 模式,但它并不适用于所有情况;
- 在某些情形下可以用一个
const对象来表达枚举变量
举例:
const myObject = { shorthandMethod() { // ... } // instead of // method: function () { // ... // } } // Method shorthand is worse herein. new Vue({ render: (h) => h(App) }).$mount('#app') // Enumerations const Option = { // The option used shall have been the first. FIRST_OPTION: 1, // The second among two options. SECOND_OPTION: 2 }
-
类型
- 私有域应该要以
$_开头; - 在构造函数
constructor完成后,不应该再添加或者删除属性; - 不要使用和操作
prototype,推荐使用class - 不推荐使用
getter和setter,原因很复杂,请使用普通方法;
举例:
class Foo { constructor() {} // private method $_computeBar() {} // public method getBar() {} }
- 私有域应该要以
-
函数
- 推荐在大部分情况下使用箭头函数,因为箭头函数更简洁,并且简化了
this的作用域; - 使用“剩余函数”(rest parameter)而非直接使用
arguments。剩余函数可以用...进行解构。
- 推荐在大部分情况下使用箭头函数,因为箭头函数更简洁,并且简化了
-
流程控制
- 循环
- 推荐使用
forEach等一系列循环操作; - 需要提升性能时,使用
for ... of操作是最高效的 for ... in循环只能在dict这一类的对象上用
- 推荐使用
- 异常(Exception):自定义异常提供了一种传达其他错误信息的好方法。 应该在原生“错误”类型不足的地方定义和使用它们。
- 循环
- 标识符:ASCII 码字母和数字,下划线以及美元符号;
- 给出一个描述性的名称。不要担心节省水平空间。
- 不要使用你的项目之外的读者不熟悉的缩写,也不要删除单词中的字母。
举例:
// Bad
n // Meaningless.
nErr // Ambiguous abbreviation.
nCompConns // Ambiguous abbreviation.
wgcConnections // Only your group knows what this stands for.
pcReader // Lots of things can be abbreviated "pc".
cstmrId // Deletes internal letters.
kSecondsPerDay // Do not use Hungarian notation.
// Good
errorCount // No abbreviation.
dnsConnectionIndex // Most people know what "DNS" stands for.
referrerUrl // Ditto for "URL".
customerId // "Id" is both ubiquitous and unlikely to be misunderstood.- 类名:
- 首字母大写的驼峰规则
- 名称应该用名词或者名词短语
- 方法名:
- 首字母小写的驼峰规则
- 名称应该用动词或动词短语
- 枚举名:
- 首字母大写的驼峰规则
- 名称应该用单数名词,每个枚举变量中的枚举量应该被命名为这种风格:
CONSTANT_CASE
- 常量名:
CONSTANT_CASE,单词大写并用下划线分隔- 名称通常为名词,或者名词短语
- 深度不变的变量是常量,类似
const array = []并不属于常量; - 推荐使用 immutable-js
- 其他
- 首字母小写的驼峰规则
- 包括非常量、参数、局部变量等
驼峰命名法
- 先将结果转化成多个词:比如
YouTube先拆开为You Tube - 除了第一个词以外,其余每个词的首字母为大写,其他字母都转化为小写。
- 串联
'XML HTTP request' -> 'XmlHttpRequest', not 'XMLHTTPRequest'
'new customer ID' -> 'newCustomerId', not 'newCustomerID'
'supports IPv6 on iOS' -> 'supportsIpv6OnIos', not 'supportsIPv6OnIOS'
'YouTube importer' -> 'YouTubeImporter', not 'YoutubeImporter'-
类/组件的注释风格:
/** * Description of class * @implements {Iterable<string>} */ class MyFancyTarget extends EventTarget { /** * @param {string} arg1 An argument that makes this more interesting. * @param {Array<number>} arg2 List of numbers to be processed. */ constructor(arg1, arg2) { // ... } }
-
函数注释风格:
/** * Description of function * @param {number} arg1 The first argument. * @param {number} arg2 The second argument. * @param {...number} rest The remainder of arguments are all numbers. * @returns {number} The result */ function func(arg1, arg2, ...rest) { // ... }
-
块注释风格
// xxx // yyy // ...
-
变量注释风格
/** * Some description. * @type {Array<number>} */ const data = [] /** * An enum with two options. * @enum {number} */ const Option = { FIRST_OPTION: 1, SECOND_OPTION: 2 }
-
弃用
- 弃用的类/接口应该用
@deprecated进行标注 - 弃用评论必须包含简单明了的指导,以便人们修复调用栈。
- 弃用的类/接口应该用
在发起 Pull Requests 的同时,需要按照以下模板填写当前 PR 的描述:
### This is a ...
- [ ] New feature (addNodes/addLinks)
- [ ] Other (documents)
### Description
### Self check
- [x] Test passed or not need
- [x] Doc is ready or not need
- [x] Demo is provided or not need
### Additional Plan?
> If this PR related with other PR or following info. You can type here.