To be aware of easy-to-read code

Be aware of easy-to-read code

If you're not careful, it tends to be redundant code, so for awareness We have summarized the points to improve readability.

Simplify conditional expressions

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;
}

Nesting too deep

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;
}

Inappropriate variable or method name

Declared according to naming convention

Snake case

java


String user_name = "Taro"

Lower camel case

java


String userName = "Taro"

Upper camel case

java


String UserName = "Taro"

Chain case

java


String user-name = "Taro"

Automatically create variable names using codic

Awareness of scope scope

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);
  }
}

Code integration

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

To be aware of easy-to-read code
Things to be aware of when writing code in Java
Things to be aware of when writing Java
Things to be aware of when using devise's lockable
[Java] Things to be aware of when outputting FizzBuzz
I want to be aware of the contents of variables!
5 things new programmers should be aware of
A collection of patterns that you want to be aware of so as not to complicate the code
[Ruby] Code to display the day of the week
Java Servlet should be aware of multithreaded environment
[Beginner] Points to be aware of after Java exercises / Inheritance / Abstract method [Note 26]
[Rails] When using ajax, be aware of "CSRF measures".
Be aware of garbage collection and avoid memory leaks
Port C code with a lot of typecasts to Swift
Java 14 new features that could be used to write code
[Rails] Where to be careful in the description of validation
Be sure to compare the result of Java compareTo with 0
How to write good code
About types of code coverage
Implementation of unit test code
Is it easy for the user to use when implementing general-purpose functions? Let's be aware of
[With sample code] Basics of Spring JDBC to learn with Blog app