Write code that is easy to maintain (Part 3)

1. What to talk about this time

2. Technique collection

2.1. Write formulas for constants and variables if there is a basis

Consider, for example, a constant that holds an upper limit on the amount of time that allows some processing.

Before correction


public static final long ACCEPT_TIME_LIMIT = 259200000L;

how is it? Do you know what you mean by just looking at this number?

Revised


public static final long ACCEPT_TIME_LIMIT  = 3 * 24 * 60 * 60 * 1000;

What if it is written like this? At 24 * 60 * 60, you can see "the number of seconds in a day", and when it is multiplied by 1000, it is "milliseconds", and when it is multiplied by 3, it is "a millisecond that represents 3 days". I can imagine. When you have a value as a constant, you tend to write only the value of the final result of some calculation. By disassembling this properly and writing it, it is easy for the reviewer to match it with the specifications at the time of code review, and the meaning of the numbers can be understood just by looking at it at the time of maintenance in the future.

2.2. Let's devise a sequence of comparison operators that represent ranges

Before correction


//Process only when 100 or more and 500 or less
if(value >= 100 && value < 500){
  //processing
}

Revised


//Process only when 100 or more and 500 or less
if(100 <= value && value < 500){
  //processing
}

When humans recognize numbers, imagine a number line where the numbers are smaller toward the left and larger toward the right. Therefore, when indicating the range of numbers, readability can be improved by exchanging them in the order of the number lines.

However, for some people, it is easier to understand in the subject predicate format in the form of "value is 100 or more and value is less than 500". It's best to use the one that is easy to understand for each organization.

2.3. When creating a method that returns List or Map, do not return null as much as possible

Before correction


public List<Long> getList(){

  if(XXX == 0){
    return null;
  }

  List<Long> list = new ArrayList<>();
  for(i = 0; i < XXX; i++){
    list.add(YYY);
  }

}

Revised


public List<Long> getList(){

  List<Long> list = new ArrayList<>();
  for(i = 0; i < XXX; i++){
    list.add(YYY);
  }

  return list;

}

When you use a method that returns a List or Map of something, the recipient will be different depending on whether the method returns null or not. If you never return null, you don't have to write `if (list! = null)` every time, and you want to know if list is empty `` `list.size ( ) ``` Is enough. Unless there is a specific reason why nullness is important, we believe that methods that make users aware of nulls can improve readability.

Recommended Posts

Write code that is easy to maintain (Part 4)
Write code that is easy to maintain (Part 3)
Let's write a code that is easy to maintain (Part 2) Name
"Let's write versatile code that is easy to use over time" (nth time)
Code that is difficult to debug and parse
Think of test code that is easy to understand through Comparator testing
How to write code that thinks object-oriented Ruby
Easy to maintain FizzBuzz
How to write good code
Java 14 new features that could be used to write code
[Java] Code that is hard to notice but terribly slow
Function is very easy to use
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
How to identify the path that is easy to make a mistake
AtCoder is called TLE and talks about how to write beautiful code
How to write test code with Basic authentication
To implement, write the test and then code the process
I tried to make FizzBuzz that is uselessly flexible
Use stream to verify that SimpleDateFormat is thread unsafe