Skip to content

Instantly share code, notes, and snippets.

Nya NyaGarcia

Block or report user

Report or block NyaGarcia

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@NyaGarcia
NyaGarcia / extracting-function-logic.js
Last active Oct 10, 2019
Extracting logic from a complex function
View extracting-function-logic.js
function startProgram() {
if (!window.indexedDB) {
throw new Error("Browser doesn't support indexedDB");
}
initDatabase();
setListeners();
printEmployeeList();
}
@NyaGarcia
NyaGarcia / complex-function.js
Last active Oct 10, 2019
An example of a complex function
View complex-function.js
function startProgram() {
if (!window.indexedDB) {
window.alert("Browser not support indexeDB");
} else {
let openRequest = indexedDB.open("store", 1);
openRequest.onupgradeneeded = () => {};
openRequest.onerror = () => {
console.error("Error", openRequest.error);
@NyaGarcia
NyaGarcia / substitute-for.js
Last active Oct 10, 2019
Substituting a for loop with Array.map and Array.filter
View substitute-for.js
function getNews(type) {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, type);
}
function getNewsContent(newsList, type) {
return newsList
.filter(newsItem => newsItem.type === type)
.map(newsItem => newsItem.content);
}
@NyaGarcia
NyaGarcia / fixing-duplication.js
Last active Oct 10, 2019
Further refactoring duplicating code
View fixing-duplication.js
function getNews(type) {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, type);
}
function getNewsContent(newsList, type) {
const news = [];
for (let i = newsList.length - 1; i >= 0; i--) {
if (newsList[i].type === type) {
news.push(newsList[i].content);
@NyaGarcia
NyaGarcia / fixing-duplication.js
Last active Oct 10, 2019
An example of how to avoid code duplication by extracting logic
View fixing-duplication.js
function getJavascriptNews() {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, 'javascript');
}
function getRustNews() {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, 'rust');
}
@NyaGarcia
NyaGarcia / duplication.js
Last active Oct 10, 2019
An example of duplicated code
View duplication.js
function getJavascriptNews() {
const allNews = getNewsFromWeb();
const news = [];
for (let i = allNews.length - 1; i >= 0; i--){
if (allNews[i].type === "javascript") {
news.push(allNews[i]);
}
}
@NyaGarcia
NyaGarcia / extracting-conditional.js
Created Oct 9, 2019
Extracting a conditional to a function
View extracting-conditional.js
function isGameLost() {
return (
remaining === 0 ||
(remaining === 1 && remainingPlayers === 1) ||
remainingPlayers === 0
);
}
// Our function is now much easier to understand:
function checkGameStatus() {
@NyaGarcia
NyaGarcia / complex-conditional.js
Last active Oct 9, 2019
A function with a complex conditional statement
View complex-conditional.js
function checkGameStatus() {
if (
remaining === 0 ||
(remaining === 1 && remainingPlayers === 1) ||
remainingPlayers === 0
) {
quitGame();
}
}
@NyaGarcia
NyaGarcia / replace-switch-map.js
Last active Oct 8, 2019
Using a Map to replace a switch statement
View replace-switch-map.js
const pokemon = new Map([
['Water', 'Squirtle'],
['Fire', 'Charmander'],
['Plant', 'Bulbasur'],
['Electric', 'Pikachu']
]);
function getPokemon(type) {
return pokemon.get(type) || 'Mew';
}
@NyaGarcia
NyaGarcia / replace-switch-object.js
Last active Oct 8, 2019
Using an object literal to replace a switch statement
View replace-switch-object.js
const pokemon = {
Water: 'Squirtle',
Fire: 'Charmander',
Plant: 'Bulbasur',
Electric: 'Pikachu'
};
function getPokemon(type) {
return pokemon[type] || 'Mew';
}
You can’t perform that action at this time.