Using meaningful name can help your code easily to to understand and more easy to debug. For example :
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println(Input r : );
double r = s.nextDouble();
System.out.println("Result : "+(r*Math.PI))
}You can know this code is circle area calculator but when you read the last line that print result of r*Math.PI. You can make your code more readable by changing names like this :
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(Input radius : );
double radius = scanner.nextDouble();
System.out.println("Result : "+(radius*Math.PI))
}This can make your code more readable.
If you use number like this in your code:
if(speed >= 0 && velocity<=299792458){ // 299792458???
...
time = time*3600 // 3600??
...
}This will make you confuse about these numbers later. It is better to set constant variable name of numbers.
public static final int LIGHT_SPEED = 299792458;
public static final int SECOND_TO_HOUR = 3600;Comments can help other people easily understand what you do in your code.
...
// Check that input speed is valid or not
if(speed >= 0 && speed <= LIGHT_SPEED) {
...
}
...Do not use name like :
double checkUserInputIsValidThenCalculateAreaOfRectangle(double width,double height) {...}You should short meaningful name instead :
double regtangleArea(double width,double height) {...}Do not copy and paste your code to do the repeatation things in different parts of your code, use methods instead. For example:
/** For encrypting String by shifting ASCII number. */
String encrypt(String in) {...}
/** Reversing method of ecrypt() for decrypting text. */
String decrypt(String in) {...}If your program using the same code for encrypt and decrypt using methods are better than copy and paste same code around the program and it make easier to change the codes only one time, not around the program.
I have one useful exercise I made it here : https://github.com/guitarpawat/codeguide/blob/master/src/Classroom.java
http://deviq.com/don-t-repeat-yourself, DevIQ's blog
https://www.codeproject.com/Articles/768052/Golden-Rules-Of-Good-OOP by Giovanni Scerra