📓 Cabinet of Ideas

Top 10 Clean Code Rules Dev Community

Top 10 Clean Code Rules 🎹🚀 - DEV Community #

Excerpt #

When writing code, it’s like writing a story. A good story is easy to read, and you understand what’s…


When writing code, it’s like writing a story. A good story is easy to read, and you understand what’s happening right away. The same goes for code! Clean code is not just for computers; it’s for other developers too. Here are the top 10 rules that will help you write code like a pro!

1. Use Meaningful Names #

Give your variables, functions, and classes names that clearly describe what they do. For example, instead of x or y, use totalScore or userName. It’s like naming your toys—you don’t call them just “toy 1” or “toy 2,” right?

2. Keep Functions Small #

A function should do only one thing and do it well. If your function is too long, it can be confusing. Imagine trying to learn 10 magic tricks at once! It’s much easier if you focus on one at a time.

3. Don’t Repeat Yourself (DRY) #

If you find yourself writing the same code over and over again, try to combine it into a single function. Think about it—if you have one awesome magic trick, why not reuse it instead of making new ones every time?

4. Use Comments Wisely #

Comments should explain why something is happening, not what is happening. The code itself should be clear enough. Imagine you wrote a secret message for your friend—you wouldn’t explain every single word, only the tricky parts.

5. Consistent Formatting #

Keep your code looking neat. Use proper spacing, indentation, and line breaks. Just like a tidy room is easier to move around in, clean and organized code is easier to read and understand.

6. Avoid Magic Numbers #

A magic number is just a random number in your code. If you use numbers, give them names or store them in variables. For example, instead of 12, use numberOfMonthsInAYear. This way, everyone knows what that number means!

7. Handle Errors Gracefully #

Don’t just let your code crash if something goes wrong—write code to handle mistakes. If you’re playing a game, you don’t want it to freeze if you press the wrong button, right? Similarly, your code should know what to do when something unexpected happens.

8. Use Clear Conditions #

When you write if statements or loops, make them easy to understand. Instead of doing everything in one line, break it up so others can follow along. It’s like giving directions—be clear, so no one gets lost.

9. Keep Things Simple #

Don’t over-complicate things. Simple code is better because it’s easier to fix and understand. Think about building a LEGO house—it’s much easier to follow the instructions than making up something overly complicated.

10. Refactor Regularly #

Always look back at your code and see if you can improve it. It’s like cleaning your room—you need to tidy it up now and then to make sure everything is in the right place!


By following these 10 clean code rules, you’ll write code that’s easy for others to read and understand. It’s like telling a great story—clear, simple, and fun!