Skip to content

Instantly share code, notes, and snippets.

View NyaGarcia's full-sized avatar
🐈

Nya NyaGarcia

🐈
View GitHub Profile
@NyaGarcia
NyaGarcia / duplication.js
Last active October 10, 2019 13:12
An example of duplicated code
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 / fixing-duplication.js
Last active October 10, 2019 15:10
An example of how to avoid code duplication by extracting logic
function getJavascriptNews() {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, 'javascript');
}
function getRustNews() {
const allNews = getNewsFromWeb();
return getNewsContent(allNews, 'rust');
}
@NyaGarcia
NyaGarcia / extracting-function-logic.js
Last active October 10, 2019 16:00
Extracting logic from a complex function
function startProgram() {
if (!window.indexedDB) {
throw new Error("Browser doesn't support indexedDB");
}
initDatabase();
setListeners();
printEmployeeList();
}
@NyaGarcia
NyaGarcia / substitute-for.js
Last active October 10, 2019 20:00
Substituting a for loop with Array.map and Array.filter
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 / complex-function.js
Last active October 10, 2019 20:01
An example of a complex function
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 / boolean-shorthands.js
Last active November 6, 2019 12:20
Conditionals with boolean shorthands
if (isWildPokemon) {
usePokeball();
}
if (isPokemon && !isSaved) {
save(pokemon);
}
@NyaGarcia
NyaGarcia / non-boolean-shorthands.js
Last active November 6, 2019 12:21
Conditionals with boolean variables without using shorthands
if (isWildPokemon === true) {
usePokeball();
}
if (isPokemon === true && isSaved === false) {
save(pokemon);
}
@NyaGarcia
NyaGarcia / return-true-false.js
Created November 7, 2019 16:31
Two functions that return true; return false;
function isWildPokemon(pokemon) {
if (pokemon.isWild) {
return true;
} else {
return false;
}
}
function isSaved(pokemonId) {
if (this.savedPokemon.some(({ id }) => id === pokemonId)) {
@NyaGarcia
NyaGarcia / fix-return-true-false.js
Created November 7, 2019 16:50
Directly returning boolean values to avoid return true return false
function isWildPokemon(pokemon) {
return pokemon.isWild;
}
function isSaved(pokemonId) {
return this.savedPokemon.some(({ id }) => id === pokemonId);
}
@NyaGarcia
NyaGarcia / if-else-if.js
Created November 7, 2019 17:38
Verbose if else if statement
function attack(pokemon) {
if (pokemon.type === 'water') {
useWaterAttack();
} else if (pokemon.type === 'fire') {
useFireAttack();
} else if (pokemon.type === 'ice') {
useIceAttack();
} else if (pokemon.type === 'electric') {
useElectricAttack();
} else {