牛马程序员,强推《屎山代码风格指南》,github 开源地址:https://github.com/trekhleb/state-of-the-art-shitcode/tree/master
这是一个你的项目应该遵循的标准代码书写准则的列表,把称为适当的标准代码。避免屎山
以一种代码已经被混淆的方式命名变量
如果我们键入的东西越多,那么就有越少的时间去思考代码逻辑等问题。
规则 1,不要以混淆方式命名变量:
// 这是一种坏的方式
let a = 10
// 这是一种比较好的方式
let age = 20
规则 2,不要混合命名
// 这是一种坏的方式
let wWidth = 640
let w_width = 640
// 这是一种好的方式
let windowWidth = 640
let windowHeight = 480
规则 3,要写注释
// 这是一种不好的例子
const cdr = 700
// 700ms的数量是根据UX A/B测试结果进行经验计算的。
// @查看: <详细解释700的一个链接>
const callbackDebounceRate = 700
规则 4,母语注释,或者英语注释
// 这是一种坏的方式
// Закриваємо модальне віконечко при виникненні помилки.
toggleModal(false)
// 这是一种比较好的方式
// 隐藏错误弹窗
toggleModal(false)
规则 5,不要把代码写成一行
把代码写在一行才显得你更愚蠢,别人写 20 行,你一行就搞定了,增加阅读难度
// 这是一种不好的方式
document.location.search.replace(/(^\?)/,'').split('&').reduce(function(o,n){n=n.split('=');o[n[0]]=n[1];return o},{})
// 这是比较很好的方式
document.location.search
.replace(/(^\?)/, '')
.split('&')
.reduce((searchParams, keyValuePair) => {
keyValuePair = keyValuePair.split('=')
searchParams[keyValuePair[0]] = keyValuePair[1]
return searchParams
}, {})
规则 6,要处理错误
无论在什么时候,发现报错了都要处理它,也要告诉任何人这里有错误。
//这是不对的
try {
// 意料之外的情况。
} catch (error) {
// tss... 🤫
}
// 这是一种正确的方式
try {
// 意料之外的情况。
} catch (error) {
setErrorMessage(error.message)
// and/or
logError(error)
}
规则 7,不要大量使用全局变量
// 这是一种不对的方式
let x = 5
function square() {
x = x ** 2
}
square() // 现在x是25
// 这是一种比较正确的方式
let x = 5
function square(num) {
return num ** 2
}
x = square(x) // 现在x是25
规则 8,不要创建你不会使用的变量
function sum(a, b, c) {
// 这是一种不对的方式
const timeout = 1300
const result = a + b
return a + b
}
// 这是一种正确的方式
function sum(a, b) {
return a + b
}
规则 9,类型可选,就要写
以 TS 为例,用规定类型,那就写类型
// 这是一种不对的方式
function sum(a, b) {
return a + b
}
// 在这里享受没有注释的快乐
const guessWhat = sum([], {}) // -> "[object Object]"
const guessWhatAgain = sum({}, []) // -> 0
// 这是一种正确的方式
function sum(a: number, b: number): ?number {
// 当我们在JS中不做置换和/或流类型检查时,覆盖这种情况。
if (typeof a !== 'number' && typeof b !== 'number') {
return undefined
}
return a + b
}
// 这个应该在转换/编译期间失败。
const guessWhat = sum([], {}) // -> undefined
规则 10,写永远到达的代码
// 这是比较不对的方式
function square(num) {
if (typeof num === 'undefined') {
return undefined
} else {
return num ** 2
}
return null // 这就是我的"Plan B".
}
// 这是一种比较正确的方式
function square(num) {
if (typeof num === 'undefined') {
return undefined
}
return num ** 2
}
规则 11,不要使用三角法则,缩进要一致
// 这是一种错误的方式
function someFunction() {
if (condition1) {
if (condition2) {
asyncFunction(params, (result) => {
if (result) {
for (;;) {
if (condition3) {
}
}
}
})
}
}
}
// 这是一种正确的方式
async function someFunction() {
if (!condition1 || !condition2) {
return
}
const result = await asyncFunction(params)
if (!result) {
return
}
for (;;) {
if (condition3) {
}
}
}
规则 12,不要混合缩进
// 这种方式错误
const fruits = ['apple',
'orange', 'grape', 'pineapple'];
const toppings = ['syrup', 'cream',
'jam',
'chocolate'];
const desserts = [];
fruits.forEach(fruit => {
toppings.forEach(topping => {
desserts.push([
fruit,topping]);
});})
// 这种方式很正确
const fruits = ['apple', 'orange', 'grape', 'pineapple']
const toppings = ['syrup', 'cream', 'jam', 'chocolate']
const desserts = []
fruits.forEach((fruit) => {
toppings.forEach((topping) => {
desserts.push([fruit, topping])
})
})
该文提出了一套代码编写准则,包括清晰命名变量,避免混合命名,添加有意义的注释,使用母语注释,避免一行代码,正确处理错误,减少全局变量使用,避免未使用的变量,明确类型,确保所有路径都有返回值,保持缩进一致性,不混合缩进,版本锁定,限制函数长度,编写测试,保持风格统一以及删除冗余代码。这些指导原则旨在提升代码质量和可读性。
3万+





