[JAVA] Let's write a code that is easy to maintain (Part 2) Name

1. What to talk about this time

2. Chat

We received a lot of opinions from the article Last time, "Let's write code that is easy to maintain (Part 1)". Thank you! It's a little personal story, but I'm a 30-year-old young engineer, 6 years after joining the current company. Since I majored in chemistry at university and graduate school, my programming history is almost the same as my hiring history. I'm going to spell out the correct answer now, but I still can't wipe the feeling of a frog in the well. It will be very educational if you can comment on the mistakes in the content, better methods, and different perspectives. Please do.

3. Technique collection

3.1. Do not use middle school or higher English for method and variable names

Since the characters used in programming are basically alphabets, it is more comfortable to use English words. However, if you use English words and think that you don't have them, the code may be difficult to maintain.

For example, if you were to name a variable that had the following meaning, what would you name it?


Consider a class that represents a business report.


Of course, there is no 100 correct answer. By the way, if you simply translate it into English with google translate ...

have become.

・ ・ ・

I think it would look like this.

The English word Implementation will not be used in junior high school. (This time, I looked at the English words on this site.) If you can't read this as "implemented" at a glance , Should also be a project member. If you limit it too much, it will be difficult to make "people who understand", but I think that it may be difficult to say that it is more difficult to study English words for those who do not. The compromise is "about junior high school English". Of course, even if it is a difficult word, if it is often used in the industry, I think you should use it.

By the way, it is not recommended to easily put serial numbers in the name (date_01 etc.). If you have to decide a huge number of variable names and they are closely related to each other so that you do not feel uncomfortable even if you assign serial numbers, if not, you will see "This variable What does it mean? ”Is hard to keep up with. (You can understand it to some extent by looking at the comments, but conversely, you can't understand unless you bother to look at the comments.)

3.2. For names with two or more words, be aware of the order

In section 3.1, the variable name I think was "date_XXX". In this section, I'm going to talk about whether to bring the "date" to the front or the back. This answer, of course, depends on the occasion. So, I would like to touch on the way of thinking that I have as a guideline.

First of all, the most important thing for me is "Which unit is easy to see / understand when imagining possible improvements in the future?" Consider the example in Section 3.1.

In this example, imagine a future improvement that thinks like "I want to sort by date. The date is a date, but what kind of date did this object have? Why is it easy to sort?" Did. On the contrary, "I already have a report creation date, but I would like to add the author as well. Oh, I want a business implementer, and I want a decision-maker! You can imagine the improvement. In that case, it may be easier to use if it is organized by the axes of create_XXX and jisseki_XXX.

There is no clear answer to this, but it's bad not to think about it at all, so please take it as a reminder. I think it's a waste of time to spend two or three hours deciding on this name.

3.3. Let's name it according to the contents

What kind of method do you imagine when you see the method names below? By the way, please think that int categoryId that appeared for the first time is the key value of the category for classifying a large number of Items. And imagine a part of the logic processing of the screen that there is a pull-down called "Category" on the web screen where the Item list is displayed, and if you change it, it will be narrowed down to the Item list of that category and redisplayed.

 public static List<String> getListItemTitle(int categoryId) {
  //Processing content

  }

"I wonder if I will access the DB and get a list of Items using the categoryId as a key" or "I wonder if only the items of the corresponding categoryId will be extracted from the ItemList that has already been acquired as an object". I think you can imagine that kind of thing.

But in fact, in this method, in addition to such a list acquisition SELECT, what if you ask "I am running an INSERT in the search history table that this user searched with this category ID"? Isn't it "Eh? Are you updating the DB ?!"

Certainly, I think there is a requirement that when you narrow down by category and perform SELECT, you always want to register the history and execute it as a pair. But if so, somewhere in the method name you should be able to tell that you are also doing an INSERT. If you don't know what the method is doing by just looking at the name, you may unintentionally reuse it in future improvements and create strange DB records.

I think that "then the method name will be longer, right?", But rather than the name "I don't have important information because I want to shorten it", the name "It contains enough information but is long" However, it is very easy to maintain. However, I don't want to allow 100 characters and 200 characters, so moderation is important.


Next time, I will continue talking about names.

Recommended Posts

Let's write a code that is easy to maintain (Part 2) Name
Write code that is easy to maintain (Part 1)
Write code that is easy to maintain (Part 4)
Write code that is easy to maintain (Part 3)
"Let's write versatile code that is easy to use over time" (nth time)
Write code that is difficult to test
How to identify the path that is easy to make a mistake
How to write code that thinks object-oriented Ruby
Easy to maintain FizzBuzz
Easy Null Check-I want to give you a chance to write good code. 6 [C # refactoring sample]
Java 14 new features that could be used to write code
[Java] Code that is hard to notice but terribly slow
The story of making a binding for libui, a GUI library for Ruby that is easy to install
Let's write a test code for login function in Spring Boot
Let's make a custom_cop that points out the shaking of the name
Function is very easy to use
How to write a ternary operator
[RSpec] How to write test code
AtCoder is called TLE and talks about how to write beautiful code
Write a third If statement that is neither an if statement nor a ternary operator
A memo that enabled VS Code + JUnit 5 to be used on Windows 10
Created a library that makes it easy to handle Android Shared Prefences
[Small story] Misleading method name (a story that wasted time due to setScale.