Characteristics of a Clean Code

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 int 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 Defination

}

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.