"do one thing and do it well".
dedicate time not only to implementation, but also to a correct structure of your functions
https://rainsoft.io/the-art-of-writing-small-and-plain-functions/?utm_source=codropscollective
Measure seven times, cut once.
假设一个场景:当一个功能函数需要通过返回"数组","map","简单js对象"去计算他的特性的值。
* 1 point for null or undefined
* 2 points for a primitive type
* 4 points for an object or function.
primitive type:简单键值对对象
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
getCollectionWeight()
Now the goal is to split the big function into smaller, independent and reusable ones
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
if (item == null) {
return sum + 1;
}
if (typeof item === 'object' ||
typeof item === 'function') {
return sum + 4;
}
return sum + 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);
}
else if (collection instanceof Map) {
collectionValues = [...collection.values()];
} else {
collectionValues = Object.keys(collection)
.map(function (key) {
return collection[key];
});
}
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 getMapValues(map) {
return [...map.values()];
}
function getPlainObjectValues(object) {
return Object.keys(object)
.map(function (key) {
return object[key];
});
}
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);
}
function getCollectionWeight(collection) {
return getCollectionValues(collection)
.reduce(reduceWeightSum, 0);
}
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);
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);
}
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 getMapValues(map) {
return [...map.values()];
}
function getPlainObjectValues(object) {
return Object.keys(object)
.map(function (key) {
return object[key];
});
}
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);
}
function getCollectionWeight(collection) {
return getCollectionValues(collection)
.reduce(reduceWeightSum, 0);
}
filter-nav.js
enterOneFilter()
enterTwoFilter()
enterThreeFilter()
......
map.js
createPoint()
setValObj.attr({'businessId':business_id});
setValObj.attr({'share-url':shareUrl});
setValObj.attr({'share-pic':sharePic});
setValObj.attr({'share-con':shareContent});
setValObj.attr(
{'businessId':business_id},
{'share-url':shareUrl},
{'share-pic':sharePic},
{'share-con':shareContent}
);
judgeUrlOut:function(type,currPoint,oldImgName){
if(type == "service"){
if(oldImgName == 'servicediamond'){
currPoint.attr('src', ZBJMap.mapUrl.serviceDiamond);
} else if(oldImgName == 'servicenormal'){
currPoint.attr('src', ZBJMap.mapUrl.serviceNormal);
} else if(oldImgName == 'servicecrown'){
currPoint.attr('src', ZBJMap.mapUrl.serviceCrown);
} else if(oldImgName == 'servicet'){
currPoint.attr('src', ZBJMap.mapUrl.serviceT);
}
} else if(type == "business"){
if(oldImgName == 'businessdiamond'){
currPoint.attr('src', ZBJMap.mapUrl.businessDiamond);
} else if(oldImgName == 'businessnormal'){
currPoint.attr('src', ZBJMap.mapUrl.businessNormal);
} else if(oldImgName == 'businesscrown'){
currPoint.attr('src', ZBJMap.mapUrl.businessCrown);
} else if(oldImgName == 'businesst'){
currPoint.attr('src', ZBJMap.mapUrl.businessT);
}
}
},
switch case
switch只计算一次值 然后都是test,jmp, if...else 是每个条件都要计算一遍的.