If you're not careful, it tends to be redundant code, so for awareness We have summarized the points to improve readability.
java
if (a == 1 && b == 2 && c == 3 && d == 4){
System.out.println("test");
}
//Wrap a conditional expression in an if statement
if (a == 1 &&
b == 2 &&
c == 3 &&
d == 4){
System.out.println("test");
}
//Create methods to improve readability
if (output()){
System.out.println("test");
}
private boolean isValidType(){
//processing
return true;
}
java
//NG
if(a == 1){
if(b == 2){
if(c == 3){
if(d == 4){
if(e == 5){
System.out.println("test");
}
}
}
}
}
//OK
////Create the same method to improve readability
if (isValidType()){
System.out.println("test");
}
private boolean isValidType(){
//processing
return true;
}
Declared according to naming convention
java
String user_name = "Taro"
java
String userName = "Taro"
java
String UserName = "Taro"
java
String user-name = "Taro"
java
//NG
class Calculator {
public static int num = 0;
public static void alterNumber(int _num) {
num = _num;
}
public static void main(String args[]) {
alterNum(23);
}
}
//OK
//Can only be executed within a class due to access control
class Calculator {
private static int num = 0;
private static void alterNumber(int _num) {
num = _num;
}
public static void main(String args[]) {
alterNum(23);
}
}
java
//NG
public class StandAction extends BaseAction {
public void Stand(Account account) {
if(account.getFirstName() == null || account.getFirstName().length()== 0) {
return;
}
if(account.getLastName() == null || account.getLastName().length() == 0) {
return;
}
}
}
//OK
//Added methods to improve readability
public class Stand extends BaseAction {
public void Stand(Account account){
if(isEmpty(account.getFirstName())){
return;
}
if(isEmpty(account.getLastName())) {
return;
}
}
public boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
}
Recommended Posts