Friday, August 7, 2020

Clean Code Principles - Must Know

 


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):


As the name suggests DRY (don't repeat yourself) means don't write duplicate code, instead use Abstraction to abstract common things in one place. 
If you have a block of code in more than two places consider making it a separate method, or if you use a hard-coded value more than one time make them public final constant. The benefit of this Object-oriented design principle is in maintenance.

D.R.Y
In short Avoid duplication in the code.

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


No matter what your style of coding is, it should follow one rule: 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

Sometimes, as developers, try to think way ahead, into the future of the project. We do code for some extra features "just in case we need them" and overthinking. Eventually, we need them". Just one word for that: Wrong!
wrong

You didn't need it and you don't need it and sometimes "You Aren't Gonna Need it"


YAGNI is a principle behind the extreme programming (XP) practice of “Do the Simplest Thing That Could Possibly Work”.

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. 

4 comments:

  1. Great Heena. precise and valuable content.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. waiting for SOLID principles article now

    ReplyDelete

ElasticSearch vs Apache SolrCloud

  Elasticsearch and Apache SolrCloud are both powerful, distributed search engines built on top of Apache Lucene. Both Elasticsearch and S...