Discover the secrets to crafting pristine code by implementing these practical and insightful tips that will elevate your coding skills.
Clean Code is an essential thing that all programmers should be concerned about, as we spend 10x more time reading than writing code, so when we need to write, we have to write clean code.
What is the Clean Code?
Before we start, what is clean code?
Clean code always looks like it was written by someone who cares. There is nothing obvious that you can do to make it better. All of those things were thought about by the code's author, and if you try to imagine improvements, you're led back to where you are, sitting in appreciation of the code someone left for you—code left by someone who cares deeply about the craft.
Michael Feathers
Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer's intent but rather is full of crisp abstractions and straightforward lines of control.
Grady Booch
10 Tips to Write Clean Code
Now that you already know what clean code is, let's go to some tips 😄
Write meaningful names. Be careful with variables, methods, and class names. They should express what they are.
Write small methods. If you can't read your entire method on a small screen, probably you should break it.
Keep your code with just one responsibility. If your classes or methods are doing more than one thing, you likely should refactor them.
Do not include comment lines in your code. Do not comment on the lines that have code, as the code should be enough. But if you are creating an API, you should document your code, so be careful about the difference between commenting and documentation.
DRY, don't repeat yourself. Remove duplicated code.
Don't create magic values. Use Constants when you have a magic number. Magic value is the value that means nothing without context.
Avoid multiple arguments. If you have more than 3 arguments, you should probably refactor it. Use a context class instead of these multiple arguments.
Use Sonar or a lint tool. With this type of tool, you can fix small things that you probably didn't notice, and guarantee a standardized code.
Low coupling and high cohesion. You should know the paradigm of your programming language very well to be able to create code with low coupling and with high cohesion.
Create unit test. Unit testing will help you maintain your code better and provide more security for maintenance.
Conclusion
Writing clean code is the key to having good software. It is crucial to write code carefully and to take care of it. Remember, you spent more time reading than writing code, so do it very well.
References
Clean code - Robert Cecil Martin
Working Effectively with Legacy Code - Michael Feathers