DEV Community

Cover image for Top 10 Clean Code Rules 🎨🚀
Anik Dash Akash
Anik Dash Akash

Posted on • Updated on

Top 10 Clean Code Rules 🎨🚀

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!

you can follow me on GitHub

Top comments (13)

Collapse
 
martinbaun profile image
Martin Baun

In general I try to make my code readable and clean in the sense that it follows a consistent pattern that others can follow and replicate. (edit) patterns like SOLID can help accomplish that goal. Great tips, always stay learning! :)

Collapse
 
anikakash profile image
Anik Dash Akash

Best wishes keep maintaining best practices

Collapse
 
martinbaun profile image
Martin Baun

Thank you! :)

Collapse
 
pinotattari profile image
Riccardo Bernardini

For example, instead of 12, use numberOfMonthsInAYear. This way, everyone knows what that number means!

Moreover, it will be easier to change when the number of months will change. (Who knows? Maybe a new French Revolution will introduce 13 28-day months) 😉 (Sorry, I couldn't resist)

Collapse
 
hectorw_tt profile image
Hector Williams

Good article!!!! I have a question though.Does refactoring violate the open-closed principle of the SOLID principles?

Collapse
 
anikakash profile image
Anik Dash Akash

@hectorw_tt No refactoring does not violate the open-closed principle of the SOLID. The OCP says software should be "open for extension but closed for modification." When you refactor, you're not adding new features or changing the code's behaviour. Instead, you're just improving the existing code to make it cleaner or more efficient without altering how it works. So, refactoring is still "closed for modification" because it doesn't change the functionality, it just makes the code better.

Collapse
 
andrewbaisden profile image
Andrew Baisden

Those are great tips. I'm doing this most of the time I work on projects.

Collapse
 
anikakash profile image
Anik Dash Akash

@andrewbaisden that's great. I'm open to work and collaboration feel free to knock me.

Collapse
 
ezekiel_77 profile image
Ezekiel • Edited

Personally the hardest tip for me to maintain is keeping functions small and DRY principles especially when your code interacts with external objects like an API or a network

Collapse
 
daniel27 profile image
Daniel D

Every point is to the point!

Collapse
 
anikakash profile image
Anik Dash Akash

Thanks

Collapse
 
eustachi0 profile image
Eustachio

I love this article. Thanks for sharing.

Collapse
 
anikakash profile image
Anik Dash Akash

Thanks