DEV Community

Cover image for DRY, KISS & YAGNI: Clean Code for Kids (with React.js)
Werliton Silva
Werliton Silva

Posted on

DRY, KISS & YAGNI: Clean Code for Kids (with React.js)

๐Ÿ‘ง๐Ÿฝ๐Ÿ‘ฆ๐Ÿพ Once upon a time in Codeville...

There were three young coders: Folden, Kevin, and Tchaca.

They were best friends and loved building things with code - especially React.js!

One sunny day, they decided to build a magical React Treehouse App.

But before they started, their wise old mentor, Mr. Byte, gave them 3 magic rules:


๐ŸŒฑ Rule 1: DRY - โ€œDonโ€™t Repeat Yourselfโ€

"Every time you repeat code," said Mr. Byte, "a bug fairy grows stronger!"

What it means:

Write your code once, and reuse it when you can.

โŒ Bad Example (Too much repeating!):

// Repeating the same button!
<button>Play</button>
<button>Play</button>
<button>Play</button>
Enter fullscreen mode Exit fullscreen mode

โœ… Good Example (DRY code):

const PlayButton = () => <button>Play</button>;

function App() {
  return (
    <>
      <PlayButton />
      <PlayButton />
      <PlayButton />
    </>
  );
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿญ Rule 2: KISS - โ€œKeep It Super Simpleโ€

"Simple code is like LEGO - easy to snap together!" said Mr. Byte.

What it means:

Make your code easy to read and understand. Donโ€™t try to be fancy!

โŒ Bad Example (Too complicated for no reason):

const Calc = ({ a, b }) => (((a + b) * 2) / 4) + a - b;
Enter fullscreen mode Exit fullscreen mode

โœ… Good Example (KISS version):

function calculateMagicNumber(a, b) {
  const sum = a + b;
  const doubled = sum * 2;
  const quarter = doubled / 4;
  const result = quarter + a - b;
  return result;
}

Enter fullscreen mode Exit fullscreen mode

๐Ÿฃ Rule 3: YAGNI - โ€œYou Arenโ€™t Gonna Need Itโ€

"Donโ€™t build a rocket if you just need a bike," laughed Mr. Byte.

What it means:

Only write the code you actually need - not what you might need later.

โŒ Bad Example (Too much code too soon!):

function App() {
  const [userRole, setUserRole] = useState("admin"); // Not used yet
  const [theme, setTheme] = useState("light");       // Not used either

  return <h1>Welcome to the Treehouse!</h1>;
}

Enter fullscreen mode Exit fullscreen mode

โœ… Good Example (YAGNI version):

function App() {
  return <h1>Welcome to the Treehouse!</h1>;
}
Enter fullscreen mode Exit fullscreen mode

๐ŸŽ‰ The Result

By using DRY, KISS, and YAGNI, the friends finished their React Treehouse app in no time!
It was clean, fast, and everyone in Codeville loved it.

โœจ And remember: Clean code is kind code - for your future self and your friends.

๐Ÿง™ Summary for Young Wizards

Rule What It Means Superpower
DRY Donโ€™t repeat your code Makes your code tidy ๐Ÿงน
KISS Keep it super simple Easy to understand ๐Ÿง 
YAGNI Only build what you need now Saves time and energy ๐Ÿ’ช

Happy coding, little React hero! ๐ŸŒˆ๐Ÿ‘ฉ๐Ÿฝโ€๐Ÿ’ป๐Ÿ‘ฆ๐Ÿปโ€๐Ÿ’ป

Made with โค๏ธ by Mr. Byte.

Top comments (0)