Wittgenstein
But here are some of mine
Caveats:
Programming should be
Hackathons, POCs, throwaway code is
fun
exempt
I am opinionated. Some of my opinions are controversial
Principles:
var result = [];
_.each(rums, function(rum){
if(rum.year < 1918){
result.push(rum);
}
});
_.filter(rums, function(rum){
return rum.year < 1918;
});
_.each(rums, function(rum){
if(rum.year < 1918){
rum.isDelicious = true;
}
});
_.map(rums, function(rum){
rum.isDelicious = rum.year < 1989;
return rum;
});
function getAvailbleYears(rum){
var rumYears = {
'Angostura': [1, 5, 7 ,12],
'Havana Club': [1, 3, 4, 7]
};
return rumYears[rum];
}
function getAvailbleYears(rum){
var rumYears = {
'Angostura': [1, 5, 7 ,12],
'Havana Club': [1, 3, 4, 7]
};
return rumYears[rum] || [];
}
Functions should always return the same type
Array
undefined
Array
Principles:
No single letter variables
No abbr in function names
var d = 5;
var stateList = ['good','bad'];
var open = true;
var elapsedTimeInDays = 5;
var possibleStates = ['good','bad'];
var isOpen = true;
Function names should imply a return type
function pending(){}
function orgs(){}
function states(){}
function calculatePercentage(){}
function isPending(){}
function hasOrganizations(){}
function getStates(){}
function getPercentage(){}
Variable names should be descriptive
Principles:
if(~'human'.indexOf('u')){
// When is this executed?
}
if(_.includes('human', 'u')){
// This is a lot clearer
}
function isReadable(){
return !!this.readable;
}
function isReadable(){
return Boolean(this.readable);
}
var date = +new Date()
var date = Date.now();
function getSomething(){
if(condition) {
// Long
// function
// body
// example
if(otherCondition){
return true;
}
}
return false;
}
function getSomething(){
if(!condition){
return false;
}
// Long
// function
// body
// example
return Boolean(otherCondition);
}
Avoid many nested conditions by returning as quickly as possible
function submitForm(){
var data = this.getFormData();
validateForm(data);
}
function validateForm(data){
if(isValid(data)){
augmentData(data);
}
}
function augmentData(data){
data.something = 123;
makeXHRCall(data);
}
function submitForm(){
var data = this.getFormData();
if(!isValid(formData)){
return;
}
var augmentedData = augmentData(data);
makeXHRCall(data);
}
function augmentData(data){
data.something = 123;
return;
}
function isProper(sausage){
if(sausage.isMadeOfChicken()){
return false;
} else {
return true;
}
}
function isProper(sausage){
if(sausage.isMadeOfChicken()){
return false;
}
return true;
}
If you need an else in your function then your function does too much.