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!