###### 如何写出小而清晰的函数？（JS 版）

1. 函数应当很小，非常小

null 或者 未定义变量计 1 点。

Step 0: 最初的大函数

function getCollectionWeight(collection) {
let collectionValues;
if (collection instanceof Array) {
collectionValues = collection;
} else if (collection instanceof Map) {
collectionValues = […collection.values()];
} else {
collectionValues = Object.keys(collection).map(function (key) {
return collection[key];
});
}
return collectionValues.reduce(function(sum, item) {
if (item == null) {
return sum + 1;
}
if (typeof item === ‘object’ || typeof item === ‘function’) {
return sum + 4;
}
return sum + 2;
}, 0);
}
let myArray = [null, { }, 15];
let myMap = new Map([ [‘functionKey’, function() {}] ]);
let myObject = { ‘stringKey’: ‘Hello world’ };
getCollectionWeight(myArray); // => 7 (1 + 4 + 2)
getCollectionWeight(myMap); // => 4
getCollectionWeight(myObject); // => 2

Step 1：根据类型计算权重，抛弃那些“迷之数字”。

// Code extracted into getWeightByType()
function getWeightByType(value) {
const WEIGHT_NULL_UNDEFINED = 1;
const WEIGHT_PRIMITIVE = 2;
const WEIGHT_OBJECT_FUNCTION = 4;
if (value == null) {
return WEIGHT_NULL_UNDEFINED;
}
if (typeof value === ‘object’ || typeof value === ‘function’) {
return WEIGHT_OBJECT_FUNCTION;
}
return WEIGHT_PRIMITIVE;
}
function getCollectionWeight(collection) {
let collectionValues;
if (collection instanceof Array) {
collectionValues = collection;
} else if (collection instanceof Map) {
collectionValues = […collection.values()];
} else {
collectionValues = Object.keys(collection).map(function (key) {
return collection[key];
});
}
return collectionValues.reduce(function(sum, item) {
return sum + getWeightByType(item);
}, 0);
}
let myArray = [null, { }, 15];
let myMap = new Map([ [‘functionKey’, function() {}] ]);
let myObject = { ‘stringKey’: ‘Hello world’ };
getCollectionWeight(myArray); // => 7 (1 + 4 + 2)
getCollectionWeight(myMap); // => 4
getCollectionWeight(myObject); // => 2

getCollectionWeight() 稍微瘦了点身。

WEIGHT_NULL_UNDEFINED, WEIGHT_PRIMITIVE 还有 WEIGHT_OBJECT_FUNCTION 都是具备自文档能力的常量，通过它们的名字就可以看出各类型的权重。你就不需要猜测 1、2、4 这些数字的意义。

Step 2: 继续切分，使之具备扩展性

function getWeightByType(value) {
const WEIGHT_NULL_UNDEFINED = 1;
const WEIGHT_PRIMITIVE = 2;
const WEIGHT_OBJECT_FUNCTION = 4;
if (value == null) {
return WEIGHT_NULL_UNDEFINED;
}
if (typeof value === ‘object’ || typeof value === ‘function’) {
return WEIGHT_OBJECT_FUNCTION;
}
return WEIGHT_PRIMITIVE;
}
// Code extracted into getMapValues()
function getMapValues(map) {
return […map.values()];
}
// Code extracted into getPlainObjectValues()
function getPlainObjectValues(object) {
return Object.keys(object).map(function (key) {
return object[key];
});
}
function getCollectionWeight(collection) {
let collectionValues;
if (collection instanceof Array) {
collectionValues = collection;
} else if (collection instanceof Map) {
collectionValues = getMapValues(collection);
} else {
collectionValues = getPlainObjectValues(collection);
}
return collectionValues.reduce(function(sum, item) {
return sum + getWeightByType(item);
}, 0);
}
let myArray = [null, { }, 15];
let myMap = new Map([ [‘functionKey’, function() {}] ]);
let myObject = { ‘stringKey’: ‘Hello world’ };
getCollectionWeight(myArray); // => 7 (1 + 4 + 2)
getCollectionWeight(myMap); // => 4
getCollectionWeight(myObject); // => 2

Step 3: 优化永无止境

function getCollectionValues(collection) {
if (collection instanceof Array) {
return collection;
}
if (collection instanceof Map) {
return getMapValues(collection);
}
return getPlainObjectValues(collection);
}

function reduceWeightSum(sum, item) {
return sum + getWeightByType(item);
}

getCollectionWeight() 函数的长度大幅减少。

#### 如何编写高质量代码

2016-01-16 14:00:35

#### 《effective java》改善JAVA代码的方法 part2

2007年11月19日 3.65MB 下载

#### 如何写出健壮的Java代码

2017-07-13 11:47:36

#### 常用的js小功能

2016-05-07 09:50:07

#### 如何在论文中显示小尺寸的清晰图像

2007-08-31 10:25:00

#### 微信小程序 page.js 中的 函数调用(this, that)

2017-12-20 16:22:59

#### 如何将不清晰的扫描版pdf转为清晰的pdf或word

2018-02-08 14:32:19

#### 【教程】会声会影“分享”输出的AVI也可以既清晰又体积小

2011-10-30 09:49:45

#### JS 调用函数的5种方法

2014-06-25 10:01:33

#### Js小案例之选择水果

2017-08-29 22:48:29