Skip to content

Instantly share code, notes, and snippets.

@komplexb
Last active November 10, 2016 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komplexb/346da985d23054cc893fe6ff9e46fd67 to your computer and use it in GitHub Desktop.
Save komplexb/346da985d23054cc893fe6ff9e46fd67 to your computer and use it in GitHub Desktop.
1. functional js: extracting common protocols via https://youtu.be/kA4-b7hvWhg
// 1. extract what is common
function executeWithConnection(functionToExecute, argument) {
try {
var db = connectToDataBase(credentials());
functionToExecute(db, asSqlInsert(track));
}
catch(e) {
handleDatabaseError(e);
}
}
// 2. refactor the orignal functions
function addUser(db, user) {
execute(db, asSqlInsert(user));
}
function addTrack(db, track) {
execute(db, asSqlInsert(track));
}
// 3. call with common function
executeWithConnection(addUser, {'name': 'Byron'});
executeWithConnection(addTrack, {'title': 'The JS Blues'});
// we see a lot of this
function addUser(user) {
try {
var db = connectToDataBase(credentials());
execute(db, asSqlInsert(user));
}
catch(e) {
handleDatabaseError(e);
}
}
function addTrack(track) {
try {
var db = connectToDataBase(credentials());
execute(db, asSqlInsert(track));
}
catch(e) {
handleDatabaseError(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment