Open In App

Characteristics of a Clean Code

Last Updated : 17 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Programmers or developers usually write codes for their project or for some companies. Or some start writing code as a beginner in the programming world. In all these scenarios a nostalgic moment comes when you revisit your code after a long time. You find your own code so difficult to understand. Here are a few good practices or the characteristics of a clean code that you should follow: 

1. Naming Conventions: This is a very basic practice to be followed. Correct naming the variables and functions are very much important. Suppose someone is going to check your code then he/she should be able to understand the code at a glance.

  • One should name the variables according to the project domain.
  • One should use ‘is’ as a prefix to Boolean variables.

Example: Suppose you are creating a banking app or something that has to do with payment.

double totalBalance;        // Represents the user account balance

double amountToDebit;      // Represents the amount to charge the user

double amountToCredit;    // Represents the amount to give to the user

boolean isUserActive;

One Must Follow:

  • Camel Case for variables and data structures. Example:
String  merchantName = "John Perry";

int  integerArray[] = new int[10];
  • Screaming Snake Case for Constants. Example:
final long ACCOUNT_NUMBER = 123456;

2. Its all about methods/functions

“The more useful methods you write the better coder you are” – Mitch Tabian, CodingWithMitch

  • Use Camel Case for naming functions.
  • Name the functions on the verb-noun sounds.
  • Opening bracket of the method must be on the same line as the method name.
  • Function should only accept 1 or 2 arguments not more than that.

Example:

double getUserBalance(long int accountNumber) {

// Method Definition

}

3. File Structure: It’s very much important to maintain the structure of the project. This makes it very clean and more understandable. The structure differs from web development to mobile development but the idea remains the same. 

Example:

  

 

4. Indentation: Sometimes you write some code and that code is composed inside another or you can say the nested code. This composition is very tricky to sort and to differentiate is not easy. So it is better to use indentation. It means to put the brackets in the right place.

  

5. Logging: When it comes to writing code it doesn’t mean that you just write code and boom compiled successfully. It is far from the truth that someday or many times you will have to debug. Writing log statements helps you in those dreadful moments. Writing log statements in functions is a great choice. Most processing is done through function so having a log statement there means we can get an indication of success and failure. 

6. Avoiding self-explanatory comments: We all like to add the comment around the code. On an important note, we should avoid comments that are self-explanatory as its time consuming and useless. One should write code that explains itself. 

Example:

final double PI = 3.14; // This is pi value //

The above statement’s comment isn’t necessary. So avoid this practice.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads