Last active
November 10, 2016 17:18
-
-
Save komplexb/346da985d23054cc893fe6ff9e46fd67 to your computer and use it in GitHub Desktop.
1. functional js: extracting common protocols via https://youtu.be/kA4-b7hvWhg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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