There are so many java practises we should follow while coding.This practises help to java programmer to write better code as well as one of who read this code for understanding. In this article i will talk about most important practises for java.

1. Use Naming Conversion

Before writing any code you should specify a set of naming conventions for your project, such as how to name classes,objects,variables.If you have a team, this things should decide as a team.

Self-explanatory

A name should be understandable for everyone and it should be easy to change.As a example ed,ga names cant understand and for writer also it can be hard to find that variable in another time.If we use expDate and getAmount names these are understandable and easy to find.

Meaningful distinctions

A name should have a meaning.If it is so anyone can understand the code easily.As a example s1 and s2 are meaningless and student1 and student2 are meaningful.

Pronounceable

Since we are people, we are very good at words, names should be pronounceable as naturally as spoken language.

2.  Follow the DRY Principle

Do not repeat same code piece of code again and again.

3. Limit line length

Long horizontally code lines are hard to read and hard to edit.There foe you should practise to avoid writing long horizontal codes by breaking lines.

4. Keep the code simple

The code should be simple.If you can write code simply then  write the code simply.Do not show off your knowledge by writing complex things.You have to decide which type of code suitable for the moment.

5. Avoid Deep Nesting

if (a) {
   
  if (b) {
      
    if (c) {
        
        
        
    }
  }
}

Instead above code, you can write as,

if (a) {
   return
}
if (b) {
   return
}
if (c) {
   return

}

6. Commenting

Commenting your code more useful than ever.When someone read the code he/she can understand code easily with this comment lines. 

you can write single line of comment using "//"(double slash) and you can write multi line code using "/*-----------*/"

public class PlayASong {

public static void main(String[] args) {

//create object of Artist class and assign values

Artist art1 = new Artist("Pablo Albo","pablo@gmail.com","pop",5);

//Artist art2 = new Artist("Janitha Tharaka","tharaka@gmail.com","rock",2);

//create object from Operator class and assign values

Operator op1 = new Operator("Symon","ymon@playsong.com",123,"Accountant");

//Operator op2 = new Operator("kusal","kusal@gmail.com",345,"Accountant");

}

}

7. Consistent Indentation

Indentation supports to code to be more understandable and more attractive.

int max(int num1,int num2)
{
num1 = 5;
num2 = 2;
if(num1>num2)
{
int max = num1;
}
else
{
int max = num2;
}
return max;
}

Instead above type of code,we can write as, 

int max(int num1,int num2)

{

num1 = 5;

num2 = 2;

 

if(num1>num2)

{

int max = num1;

}

else

{

int max = num2;

}

return max;