代码规范

2019-11-22  本文已影响0人  麦曦

规范概述

规范的制定是我们长期以来对工作的积累与沉淀的产物,帮助我们更快、更好、更高效的完成繁重、复杂、多样化的任务,我们制作规范的主要目的在于:

书写格式

缩进层级

不推荐

|

switch (a) {

case 'a'``:

break``;

case 'b'``:

break``;

}

|

推荐

|

switch (a) {

case 'a'``:

break``;

case 'b'``:

break``;

}

|

分号

不推荐

|

let baz = { a: 1 }

function foo() {

return true

}

if (foo) {

bar = 0;

};

|

推荐

|

let baz = { a: 1 };

function foo() {

return true``;

}

if (foo) {

bar = 0;

}

|

空行

代码块之间空行

拖尾换行

在非空文件中,存在拖尾换行是一个常见的 UNIX 风格,它的好处是可以方便在串联和追加文件时不会打断 Shell 的提示。在日常的项目中,保留拖尾换行的好处是,可以减少版本控制时的代码冲突。

空格

操作符空格

不推荐

|

let sum=1+2

|

推荐

|

let sum = 1 + 2

|

关键字空格

不推荐

|

if``(condition) { ... }

|

推荐

|

if (condition) { ... }

|

行末空格

不推荐

|

let a = 1;

|

推荐

|

let a = 1;

|

函数命名/调用空格

不推荐

|

function func () {

// do something

}

func ()

|

推荐

|

function func() {

// do something

}

func()

|

逗号,冒号,分号空格

不推荐

|

[1,2,3]

let baz = { a:1 }

for (let a = 1;a <= 10;a++) { }

|

推荐

|

[1, 2, 3]

let baz = { a: 1 }

for (let a = 1; a <= 10; a++) { }

|

单行代码块空格

不推荐

|

function foo() {``return true``;}

if (foo) {bar = 0}

|

推荐

|

function foo() { ``return true``; }

if (foo) { bar = 0 }

|

圆括号空格

不推荐

|

getName( name );

|

推荐

|

getName(name);

|

注释首尾空格

不推荐

|

//comment

/*comment*/

|

推荐

|

// comment

/* comment */

|

空格个数

不推荐

|

const id = 1234;

|

推荐

|

const id = 1234;

|

字符串

命名

文件名

变量名/属性名

常量名

样式名

组件名/类名

函数名/方法名

项目与文件

目录结构

React Native 项目

一般而言,公共模块使用自己独立的组件目录、样式目录、图片目录(便于移植);非公共模块可以按照模块分,也可以按照页面样式图片分

【示例:非公共模块按照模块分,仅供参考】

jsbundles
├─ JDReactWorldOfTomorrow
│    ├─ common
│    │    ├─ img
│    │    │    ├─ pic.png
│    │    │    ├─ pic@2x.png
│    │    │    └─ pic@3x.png
│    │    ├─ img.js
│    │    ├─ style.js
│    │    ├─ enum.js
│    │    ├─ fetch.js
│    │    └─ utils.js
│    ├─ components
│    │    ├─ loading
│    │    │    ├─ img
│    │    │    │    ├─ prefix-pic-suffix.png
│    │    │    │    ├─ prefix-pic-suffix@2x.png
│    │    │    │    └─ prefix-pic-suffix@3x.png
│    │    │    ├─ style
│    │    │    │    └─ index.js
│    │    │    ├─ index.js
│    │    │    └─ README.md
│    ├─ pages
│    │    ├─ order
│    │    │    ├─ img
│    │    │    │    ├─ prefix-pic-suffix.png
│    │    │    │    ├─ prefix-pic-suffix@2x.png
│    │    │    │    └─ prefix-pic-suffix@3x.png
│    │    │    ├─ style
│    │    │    │    ├─ header.js
│    │    │    │    ├─ body.js
│    │    │    │    ├─ footer.js
│    │    │    │    └─ index.js
│    │    │    ├─ header.js
│    │    │    ├─ body.js
│    │    │    ├─ footer.js
│    │    │    ├─ index.js
│    │    │    └─ README.md
│    ├─ reducers
│    │    ├─ root-reducer.js
│    │    └─ index.js
│    ├─ router.js
│    └─ app.js
├─ JDReactWorldOfTomorrow.js
├─ JDReactWorldOfTomorrow.version
└─ JDReactWorldOfTomorrow.web.js

换行问题

书写顺序

函数内代码区域排序

模块js文件代码区域排序

React生命周期方法与其他方法顺序问题

旧版本 16.3之前

书写顺序

|

constructor() { }

componentWillMount() { }

render() { }

componentDidMount() { }

componentWillReceiveProps() { }

shouldComponentUpdate() { }

componentWillUpdate() { }

componentDidUpdate() { }

componentWillUnmount() { }

// 其他方法...

|

新版本 16.3及之后

生命周期官方文档
生命周期更新博客

生命周期顺序

|

// Mounting

constructor(props)

static getDerivedStateFromProps(props, state)

render()

componentDidMount()

// Updating

static getDerivedStateFromProps(props, state)

shouldComponentUpdate(nextProps, nextState)

render()

getSnapshotBeforeUpdate(prevProps, prevState)

componentDidUpdate(prevProps, prevState, snapshot)

// Unmounting

componentWillUnmount()

// Error Handling

static getDerivedStateFromError(error)

componentDidCatch(error, info)

|

书写顺序

|

constructor(props)

static getDerivedStateFromProps(props, state)

render()

componentDidMount()

shouldComponentUpdate(nextProps, nextState)

getSnapshotBeforeUpdate(prevProps, prevState)

componentDidUpdate(prevProps, prevState, snapshot)

componentWillUnmount()

static getDerivedStateFromError(error)

componentDidCatch(error, info)

// 其他方法...

|

温馨提示

上一篇 下一篇

猜你喜欢

热点阅读