Skip to content

Instantly share code, notes, and snippets.

@themonster2015
Created August 23, 2021 10:10
Show Gist options
  • Save themonster2015/6263ea1649ed6b48f9187687303b5a6e to your computer and use it in GitHub Desktop.
Save themonster2015/6263ea1649ed6b48f9187687303b5a6e to your computer and use it in GitHub Desktop.
Example 1:
const pets = ['Cat', 'Dog', 'Bird', 'Fish', 'Frog', 'Hamster', 'Pig', 'Horse' 'Lion', 'Dragon'];
// Print all pets
console.log(pets[0]);
console.log(pets[1]);
console.log(pets[2]);
console.log(pets[3]);
...
.cat {
font-family: "Times New Roman", Times, serif;
font-size: 1rem;
color: #FFF;
}
.dog {
font-family: "Times New Roman", Times, serif;
font-size: 1rem;
color: #000;
}
.dragon {
font-family: "Times New Roman", Times, serif;
font-size: 1rem;
color: #009933;
}
The code is not DRY due to multiple repeats of console.log, my version would be to loop through the pets arrat and console.log it.
The CSS file also constain repeatition. For classes that shares the same styling rules, we should group them together.
Example 2:
const greet = (message, name) => {
console.log(`${message}, ${name}!`)
}
greet('Hello', 'John');
greet('Hola', 'Antonio');
greet('Ciao', 'Luigi')
.greetings {
font-family: Arial, sans-serif;
font-size: 1.5rem;
}
.greetings.english {
background-color: #000;
color: #FFF;
}
.greetings.spanish {
background-color: #FFF;
color: #000;
}
This code is fine and following the DRY principle.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment