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.
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.
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