[JAVA] How to name variables 7 selections of discomfort


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.

1. Omitted

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

2. Subtle spelling mistakes

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

3. There is no specificity and it is difficult to understand the purpose

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

4. The meaning is reversed

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;

    isValid = true;


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.

5. I can't imagine the type

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.

6. I can't imagine singular or plural

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.

7. I purposely changed the idiomatic name

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.

in conclusion

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

How to name variables 7 selections of discomfort
How to name variables in Java
[Rails] How to change the column name of the table
How to use Java variables
How to use setDefaultCloseOperation () of JFrame
[Processing × Java] How to use variables
How to set the IP address and host name of CentOS8
Basics of Java development ~ How to write programs (variables and types) ~
How to change app name in rails
[java] Summary of how to handle char
Summary of how to write annotation arguments
How to determine the number of parallels
[Java] [Maven3] Summary of how to use Maven3
How to sort the List of SelectItem
How to use environment variables in RubyOnRails
[Spring MVC] How to pass path variables
Summary of how to select elements in Selenium
JDBC promises and examples of how to write
How to find the cause of the Ruby error
graphql-ruby: How to get the name of query or mutation in controller Note
[java] Summary of how to handle character strings
[Rails] Introduction of pry-rails ~ How to debug binding.pry
Customize how to divide the contents of Recyclerview
[Bcrypt] how to cancel presence: true of has_secure_password
[Java] Summary of how to abbreviate lambda expressions
How to deploy
How to get today's day of the week
Output of how to use the slice method
How to use enum (introduction of Japanese notation)
[Swift UI] How to disable ScrollsToTop of ScrollView
How to use JQuery in js.erb of Rails6
How to display the result of form input
How to set environment variables in the properties file of Spring boot application
How to create member variables with JPA Model
[Java] How to get the authority of the folder
How to specify index of JavaScript for statement
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to embed JavaScript variables in HTML with Thymeleaf
[Java] How to get the URL of the transition source
[Java] How to use compareTo method of Date class
How to delete / update the list field of OneToMany
How to apply HandlerInterceptor to http inbound-gateway of Spring Integration
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
[Rails, JS] How to implement asynchronous display of comments
[Java] How to extract the file name from the path
[Ruby] How to find the sum of each digit
[Ruby on Rails] How to change the column name
How to install the root certificate of Centos7 (Cybertrust)
[Java] How to get the maximum value of HashMap
I want to display the name of the poster of the comment
[SwiftUI] How to specify the abbreviated position of Text
As of April 2018 How to get Java 8 on Mac
How to set BuildConfig of library project (module) to Debug
How to use CommandLineRunner in Spring Batch of Spring Boot
Summary of Java communication API (1) How to use Socket
[Android] How to get the setting language of the terminal
Summary of Java communication API (3) How to use SocketChannel
How to prevent editTextPreference of android PreferenceFragmentCompat from breaking
Summary of Java communication API (2) How to use HttpUrlConnection
[Rails] How to get the contents of strong parameters