Developing for
Humans
mature
Great programmers are not always
engineers
"Mature" engineering has
many facets
Writing
maintainable
code
Being a
zealous
reviewer
"If a lion could speak, we could not understand him"
Wittgenstein
A lesson from the design team:
Waterbear
Be a
But here are some of mine
your own
Develop
best practices
Caveats:
Programming should be
Hackathons, POCs, throwaway code is
fun
exempt
I am opinionated. Some of my opinions are controversial
use quirks
Never
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();
->
->
->
Keystrokes are
cheap
Comprehension is
expensive
No single letter variables
No abbr in function names
Naming is
hard
but
important
var d = 5;
var stateList = ['pending','active'];
var open = true;
var elapsedTimeInDays = 5;
var possibleStates = ['pending','active'];
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
Fail
function getSomething(){
if(condition) {
something();
_.each(things, function(thing)
if(thing.widget){
if(somethingElse(thing.widget)){
doThing(thing);
}
}
});
return true;
}
return false;
}
function getSomething(){
if(!condition){
return false;
}
// Long function body
}
Avoid many nested conditions by returning as quickly as possible
fast
->
Semantic is
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 = Boolean(rum.year < 1918);
return rum;
});
Use core concepts to communicate intent
scannable
return types
function getNumberOfEmployees(state){
var employeeStateMap = {
'active': 5,
'pending': 10
}
return employeeStateMap[state];
}
function getNumberOfEmployees(state){
var employeeStateMap = {
'active': 5,
'pending': 10
}
return employeeStateMap[state] || 0;
}
Functions should always return the same type
Consistent
Nested Ternary Expressions
Else is
function isProper(sausage){
if(sausage.isMadeOfChicken()){
return false;
} else {
return true;
}
}
function isProper(sausage){
if(sausage.isMadeOfChicken()){
return false;
}
return true;
}
redundant
If you need an else in your function then your function does too much.
Keep flow control at the
highest level
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;
}
->
Human.js
dfh
By Will Demaine
dfh
- 1,417