I have many opportunities to read sources written by others (mainly sources taken from suppliers) due to my work, but I often feel uncomfortable about "how to name variables", which is one of the causes of difficulty in reading. I tried to list the ones that were expensive.
Of course, I also want to be careful about the sources I wrote so that I don't get confused later.
The patterns are listed below.
× res -> 〇result △ prop -> 〇property × pat -> 〇pattern × enz -> 〇enzyme × idx-> 〇 index (Added 2019/7/30) × tbl-> 〇 table (Added 2019/8/24)
I didn't know about it in the old days when machine resources were limited, and nowadays, readability is greatly reduced, so there is little merit to omit it. "prop" is not so uncomfortable, but from the viewpoint of consistency, "property" is less uncomfortable. When it comes to "enz", I think it's okay to omit (probably) important words that you rarely come across.
× propaty -> 〇property × canceld -> 〇canceled × spacified -> 〇specified
Although "propaty" felt strange from the beginning, it wasn't a common mistake, so it took me a while to notice that it was misspelled, and when I noticed it, my chest was refreshed. I understand that I want to make "canceld" a past form, but I want you to write it correctly. Many recent development tools also point out spelling mistakes, so you can use them.
× count -> 〇progressCount × max -> 〇progressCountMax etc
I think that there are cases where the scope is narrow, such as in a small loop or function, but if you use it for an instance variable of a large class, you have to write it a little more concretely, when reading the relevant part, "What variable It will be ".
This applies not only to primitive types, but also to class type variables. For example, as the name of the class type variable of the CalculationResult class,
× result -> 〇calculationResult
It should be concrete like. In the former, you cannot immediately imagine what the result is.
It is okay to give a boolean type variable name called isValid, but "true" is entered when it is not valid, and "false" is entered when it is valid. It can only be said that it is out of the question. I even feel convinced when this happens ...
boolean isValid = false;
if(!inputFile.exists()){
isValid = true;
}
if(isValid){
printUsage();
}
2019/7/30 postscript At a later date, I had the opportunity to look at the original source, which was named exitFlag instead of isValid. It seems to be a punch line that I changed the name to use it for another purpose, but did not change the meaning of true / false. The original developer and the modifier are different people. The common anti-pattern. Boy, keep your point of view high.
Since I often read the source while imagining how the data will be processed, I think it is desirable to know the purpose and type from the variable name at a glance. Other than primitive types, arrays, Lists, Maps, etc. are often used in data processing. List is 〇〇List (○○ is the name that represents what is stored in List), Map is 〇〇 △△ Map (○○ is the name that represents the key of Map, △△ is the name that represents the value of Map) It feels easy to understand if it is attached.
The following example is given for the Map type.
× names -> 〇 nameValidMap
It turns out that the latter is at least a map that stores the correspondence between names and something, but the former doesn't tell at all. I misunderstood it as a list, and as I read it? Suddenly, I will return to the defined part and repeat the loop of confirmation.
It is similar to the previous section, but in following the flow of data processing, it is important to understand whether the variable is "singular" data or "plural" data. Therefore, if it is singular data, it is much easier to understand if it is in the singular form, and variables that can store multiple data such as arrays and lists are in the plural form.
It is a familiar main function in Java. It is written like this.
public static void main(String[] option){
}
Do you feel uncomfortable? Yes, the name of the argument of the main function is changed to another name even though the market price is fixed as "argv".
The arguments of the main function contain the program's "arguments", not the "options" (even if they are stored as a result).
I was quite wondering where this variable called option was defined. If you want to change the name you use idiomatically, you need a very positive reason.
There are a lot of other sources that I'm wrestling with, such as "how to insert blank lines", "how to scope variables", "how to write comments", and "order of processing", but another opportunity. I want to summarize it in.
Recommended Posts