Skip to content

Instantly share code, notes, and snippets.

@derek-knox
Last active September 15, 2017 01:14
Show Gist options
  • Save derek-knox/93c2f9020a82f4165b7f32956a0ff416 to your computer and use it in GitHub Desktop.
Save derek-knox/93c2f9020a82f4165b7f32956a0ff416 to your computer and use it in GitHub Desktop.
Decreasing LOC while maintaining clarity with ternary and bracket notation (1st Approach - 6 LOC from Kyle Simpson's Deep JS Foundations)
/* 1st Approach - 6 LOC */
if(insertBefore) {
workElements[adjacentWorkEntryId].before($workEntry);
}
else {
workElements[adjacentWorkEntryId].after($workEntry);
}
/* 2nd Approach - 4 LOC (curly brace removal) */
if(insertBefore)
workElements[adjacentWorkEntryId].before($workEntry);
else
workElements[adjacentWorkEntryId].after($workEntry);
/* 3rd Approach - 2 LOC (ternary replacing if/else and bracket notation replacing repetitive code */
var method = insertBefore ? 'before' : 'after';
workElements[adjacentWorkEntryId][method]($workEntry);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment