Main menu

Pages

clean code best practices

 Hi Devs,

today i will talk about best practices to write readable and clean code .

RULE 1

you don't write code for machines, machine don't know and care about your code quality and readability it executes it.

here is some of code best practices you should follow.

1- Use consistent indentation

There is no right or wrong indentation that everyone should follow. The best style, is a consistent style.

for example if you decided that you will use 2 spaces after define variables don't change it later, if organized code that variables will be in first of file don't change it later, you should be consistent to follow your indentation to help other developers get familiar with you code. 

2-Follow the DRY Principle

DRY stands for “Don’t Repeat Yourself.

your code must not repeated many times, if you found that some of code parts are repeated take them out in new function .

For Example:

 
 
 
 
 
 
 
 

// show toast or message for one second.
Toast.show("message","1S");
//show toast for 2 second with red background
Toast.show("message","2S","red");
// this is repeated code and should be in a method.

void showToast(String message, String showTime , String color){
Toast.show(message,showTime,color);

}

3-Avoid Deep Nesting

Too many levels of nesting can make code harder to read and follow.

For example:

if(a){
  
if(b){
  ....
   if(c){
    ...
return value;
  }
}
}
//this is many nesting levels make code not readable.

//solution
if(a){
 ...
return value;
}
if(b){
...
return value;
}
if(c){
return value;
}
//try to keep code in a linear steps with little nesting

4- Limit line length

Long lines are hard to read. It is a good practice to avoid writing horizontally long lines of code.
best practice to limit line characters to 80 characters and divide long lines into multi lines.

5-File and folder structure

avoid writing all of your code in one or 2 files. That won’t break your app but it would be a nightmare to read, debug and maintain your application.
organize your code in multi folder and files for readability and simplicity of your code and files specific tasks.
Read this article about organize your code by layer or feature.

6-Naming conventions

naming convention is a rule to follow as you decide what to name your identifiers such as class, package, variable, constant, method, etc.

But, it is not forced to follow. So, it is known as convention not rule.


Class
  • It should start with the uppercase letter.
  • It should be a noun such as Color, Button, System, Thread, etc.
  • Use appropriate words, instead of acronyms.

Interface
  • It should start with the uppercase letter.
  • It should be an adjective such as Runnable,  ActionListener.
  • Use appropriate words, instead of acronyms.

Method
  • It should start with lowercase letter.
  • It should be a verb such as  print(), log().
  • If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter such as printUserInfo().

Variable
  • It should start with a lowercase letter such as id, name.
  • It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).
  • If the name contains multiple words, start it with the lowercase letter followed by an uppercase letter such as userName, emailAddress.
  • Avoid using one-character variables such as x, y, z.

Package
  • It should be a lowercase letter such as java, lang.
  • If the name contains multiple words, it should be separated by dots (.) such as java.util, or with - or _ such as user-details.

Constant
  • It should be in uppercase letters such as RED, YELLOW.
  • If the name contains multiple words, it should be separated by an underscore(_) such as MAX_PRIORITY.
  • constant may contain numbers or digits but it must not be in the first character.

Java and most of programming languages follows camel-case syntax for naming the class objects, interface object, method declaration, and variable.

If the name is combined with two words, the second word will start with uppercase letter always such as userName, ThreadKiller, logError(), etc.


Thanks and waiting your comments.

You are now in the first article
reactions

Comments