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
Self-explanatory
Meaningful distinctions
Pronounceable
2. Follow the DRY Principle
3. Limit line length
4. Keep the code simple
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
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;
}



0 Comments