Skip to content

Instantly share code, notes, and snippets.

@rachidelaid
Created January 17, 2022 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachidelaid/4ae20e1b96d9f1f5794edbad7854dbdb to your computer and use it in GitHub Desktop.
Save rachidelaid/4ae20e1b96d9f1f5794edbad7854dbdb to your computer and use it in GitHub Desktop.
Is it DRY? Exercise
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]);
...

This block of code is not DRY.

this is how you make it DRY:

const pets = ['Cat', 'Dog', 'Bird', 'Fish', 'Frog', 'Hamster', 'Pig', 'Horse' 'Lion', 'Dragon'];
// Print all pets
console.log(pets);

.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;
}

This block is not DRY as well

and this is how to make it DRY

.animals {
  font-family: "Times New Roman", Times, serif;
  font-size: 1rem;
}

.animals.cat {
  color: #FFF;
}

.animals.dog {
  color: #000;
}
.animals.dragon {
  color: #009933;
}
const greet = (message, name) => {
  console.log(`${message}, ${name}!`)
}

greet('Hello', 'John');
greet('Hola', 'Antonio');
greet('Ciao', 'Luigi')

This block of code is DRY

beacuse there is no code dublication.

.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 block of code is DRY

beacuse there is no code dublication.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment