Good Software system begins with Clean Code that is easy to understand and easy to change. It’s the idea that your code should be precise and as close to perfect as possible.
How to Implement Clean Coding Design
So, how does one create a clean code? Well, first of all, you can’t think of your project as a coding project; you have to think of it as a designing and planning process. Developers often rush into the code because they feel pressure from their managers or whomever to get the job done quickly. In contrast, a clean coder makes sure he fully understands the problem before beginning to code. There are some principles that we can follow before writing actual code.
Universal Principles to follow:
DRY (Don't Repeat Yourself):
How to Achieve DRY
To avoid violating the DRY principle, divide your system into pieces. Divide your code and logic into smaller reusable units and use that code by calling it where you want. Don't write lengthy methods, but divide logic and try to use the existing piece in your method.
KISS: Keep It Simple, Stupid
The KISS principle is descriptive to keep the code simple and clear, making it easy to understand. After all, programming languages are for humans to understand — computers can only understand 0 and 1 — so keep coding simple and straightforward. Keep your methods small. Each method should never be more than 40-50 lines.
Each method should only solve one small problem, not many use cases.
If you have a lot of conditions in the method, break these out into smaller methods. It will not only be easier to read and maintain, but it can help find bugs a lot faster.
How to Achieve KISS
To avoid violating the KISS principle, try to write simple code. Think of many solutions for your problem, then choose the best, simplest one and transform that into your code.
YAGNI: You Aren't Gonna Need It
How to Achieve YAGNI
To avoid violating the YAGNI principle, try to write code that is required right now. Make everything simple. Don't write code unnecessarily.
So make your code granular and write only the required code.
We also have SOLID principles that help to write clean code. For design and architectural perspective, we should follow SOLID principles.
I will write another blog for SOLID principles.
Conclusion:
It is not possible to follow every rule but if developer try to implement these things from the beginning then at some level, code cleaning can be achieved.
Great Heena. precise and valuable content.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletewaiting for SOLID principles article now
ReplyDeleteThanks Dev.
DeleteWill publish SOLID article soon.