Characteristics of a Clean Code - GeeksforGeeks

17 Apr, 2020
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];

Example:
String  merchantName = "John Perry";

int  integerArray[] = new int[10];

Screaming Snake Case for Constants.Example:final long ACCOUNT_NUMBER = 123456;
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 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.
gupta_shrinath
TechTips
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Add External JAR File to an IntelliJ IDEA Project?
How to Delete Temporary Files in Windows 10?
How to Convert Kotlin Code to Java Code in Android Studio?
Difference between RUN vs CMD vs ENTRYPOINT Docker Commands
How to Install Flutter on Windows?
How to Install Z Shell(zsh) on Linux?
How to Clone Android Project from GitHub in Android Studio?
How to Access Localhost on Mobile Browsers?
How to setup OpenGL with Visual Studio 2019 on Windows 10?
Autorun a Python script on windows startup