Top 10 Rules for Writing Clean Code
Top 10 Rules for Writing Clean Code
In the book "Clean Code" by Uncle Bob, he outlines guidelines and rules that developers should adhere to, particularly those with less experience. As developers gain more experience, they may justify breaking or reinterpreting some of these rules.
1.Avoid code comments.
A good code needs no comment. The variables, methods, and any other component of the code such as attributes should use easily identifiable and descriptive names.
“Code comments are smell, remove them.”
2.Dead comments or code should be deleted.
Any unused piece of code or comments should be deleted. The best place to find them is in the version control systems.
3.Incorrect behaviour at boundaries.
Boundaries should always be unit tested. No behaviour should be assumed.
4.Positive conditionals.
Positive conditionals are easier to read than negative conditionals.
5.Standard architecture, coding, and design guidelines should be followed.
All the set standard architecture, coding, and design guidelines should be adhered to. Tools should also be used to ensure that this is accurate.
6.Good and Consistent naming.
Try to call variables, methods, classes to be understandable from the caller perspective. And it should be consistent.
7.KISS principle should be applied.
Needless complexity should be avoided. With increase in complexity, some design and codes in the system become useless. All designs and codes should be kept as simple as possible.
8.Use of exceptions instead of return codes.
In exceptional cases, an exception should be thrown, when the method fails to do its intended purpose. Null or return codes shouldn’t be used.
9.Make things small. Class size and method size both matter.
Typically, method should be small and compact in size. Ideally, <100 lines of code>
.
10.Boy scout rule- Leave the campground cleaner than you found it.
Leave the campground cleaner than you found it. Don’t ask for permission to refactor the code.
Conclusion
In conclusion, writing clean code is crucial for enhancing the readability, maintainability, and overall quality of development projects. By adhering to core "Clean Code" principles—such as minimizing unnecessary comments, using clear and concise variable names, and keeping functions short and focused—developers can create more efficient and sustainable code. While some rules may be adapted with experience, understanding and applying these practices are essential for any developer aiming to improve their skills and deliver successful projects.