Last active
October 13, 2017 20:19
-
-
Save bgadrian/2f110769d0a7c20bfef04cbf6934556f to your computer and use it in GitHub Desktop.
Example for blog post: Constantly improving the codebase
This file contains hidden or 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
var currentYear = 2017 | |
//private function, fetch from DB | |
function getTodos() { | |
return [ | |
{ name: "boogy", checked: true, year: 2014 }, | |
{ name: "alfa", checked: false, year: 2017 }, | |
{ name: "back to the future", checked: false, year: 2021 }, | |
] | |
} | |
//we need a new functionality, similar with this one | |
//but instead of OLD entries we need only the future ones | |
function PublicGetOldTodos() { | |
var todosOrig = getTodos() | |
//stupid comment like "this filters the todos" | |
var todos = new Array(); | |
var length = todosOrig.length; | |
//"this is a loop" comment | |
for(var i = 0; i < length; i++) | |
{ | |
if(todosOrig[i].year < currentYear && todosOrig[i].checked == false) | |
{ | |
todos.push(todosOrig[i]); | |
} | |
} | |
//other comments | |
if (todos.length == 0) { | |
console.log("a dev forgot some code here") | |
} | |
if (isIE6) { | |
//god help us all | |
} | |
//don't overflow | |
if (todos.length > 10){ //10 is a magic number, we hate magic numbers | |
return todos.slice(0, 10) | |
} | |
return todos | |
} |
This file contains hidden or 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 removed the not used code, added parameters and extract the common functionality | |
//with the feature we need to implement | |
function getFilteredTodos(limit = 20, predicate = () => true) { | |
var todos = getTodos() | |
todos = _.filter(todos, predicate) | |
todos = _.slice(todos, 0, limit) | |
return todos | |
} | |
function PublicGetOldTodos(limit = 10) { | |
var condition = (value, index, collection) => { | |
return value.year < currentYear && value.checked == false | |
} | |
return getFilteredTodos(limit, condition) | |
} |
This file contains hidden or 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
function PublicGetFutureTodos(limit = 5) { | |
var condition = (value, index, collection) => { | |
return value.year > currentYear && value.checked == false | |
} | |
return getFilteredTodos(limit, condition) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment