What I often do when I have trouble naming with Java (etc.)

I wrote "in Java" in the title, but it's not a topic limited to a specific language. It happened that there was a lot of Java development these days, and I added "in Java" from the actual example at that time. I hope you can apply it to your own language other than Java.

So, if you write only the conclusion first, it will be like this.

- Borrow keywords from other languages. </ b> -- Borrow the naming of other libraries. </ b> * Including standard libraries in other languages

** (*'▽') If you are interested, please read on! ** **

■ About naming

Naming is very important in programming, especially in code that is read by an unspecified number of people, such as group development and open source. However, although very important, giving ** proper names ** to class and method names can be a more time-consuming task than you might think.

Anti-patterns related to naming are another big theme, so I will omit them this time, It can be a daunting task to give a good name that is easy to understand while avoiding bad naming.

With that said, I would like to introduce what I usually practice in order to ** give an appropriate name as efficiently as possible **.

◇ Thinking flow at the time of naming

I will write an article on Qiita again, so just writing what I usually do is not much different from ** Hatena Blog **. I thought, so I looked back on the naming pattern that I usually use when programming.

  1. Use a name that you usually use. *
    (Reuse the naming used in the past) *
  2. If you don't have a name you use regularly, then you start thinking about it for the first time.
  3. Take a quick look at the terms commonly used in the language. *
    (Estimated: Level that appears within one page) *
  4. Borrow keywords in other languages and names in other libraries. *
    (Specific examples will be described later) *
  5. If there is nothing you can do, why not translate it into English and name it in a nice way? *
    (If you really want to "think of a name", this is the last resort. Basically, you bring a defined word from somewhere.) *
  6. [Supplement]
    If it seems that it will take some time, leave the name temporarily, add
    FIXME, and refactor it later to proceed.
    • I think that the priority will be slightly different depending on the item, but I wonder if the basic form will be like this. *

** (*'▽') Well, I think it's normal! ** **

◇ Diversion of keywords in other languages

As I wrote in the above thinking flow, "borrowing keywords from equivalent functions in other languages" is quite convenient. Specifically, here are some examples of "diversification" that I actually did.

Example: Diverted from C # keywords in Java development.

Named the common parts that perform multithreading processing as async / await / aggregate.

When I made a multithreaded part that wraps Java's multithreaded standard implementation (java concurrency utilities) C # (strictly speaking, it was C # 3.x series) has an added function of async / await that allows you to write asynchronous processing concisely as a language syntax. As a considerable function, the naming of that area has been improved.

aggregate hasn't been added as a keyword, but it's been used as a common name for a long time.

Example: In Java 6 development, the names of additional functions after Java 6 are used.

I created a class called TryWithResources. (I understand the implementation details)

No, please upgrade the JDK version obediently www That's what I thought so, that's the cat I said a million times. However, the reality is that many development sites in Japan are still conservative. In some cases, political reasons will categorically refuse to upgrade the version. *right? * Or, ** You can upgrade the version, but then you should test all the functions of the system **. * After all, what about security risks still using JDK6? ?? I think, but ... well, it was just a customer's wish to be a god. (Free translation: I don't know anymore) *

This is a straightforward name given to a utility class that can be used as an alternative to the try-with-resources syntax to ensure reliable resource release. Of course, this is an appeal that says, "I didn't need to create such a class if I could raise it to JDK7 !!" I also wrote in javadoc, "In the future, raise it to JDK7 or higher and erase this bad class." Well, I guess he still survives without being erased ...

Example: LINQ method names are easy to pick up.

Name the object that specifies the condition predicate, Or you could name such an interface IPredicate.

The LINQ method names (although most of them are the corresponding delegate names) are also very versatile and there are many good names that are easy to crack.

◇ Advantages of naming

Borrowing a name from another implementation, especially using one that is at the keyword level, Beyond shortening the time to think of a name, there are the following merits, and this is my favorite.

-Because it is already defined, the information density is high. ――Information comes out in that word. ――If there is an official document, you can write the URL and skip the reference.

◇ Advantages of using predefined words

Especially the first merit is very big.

It is very effective to use ** defined names ** as a means of expressing systematic knowledge and information in a concise manner.

The amount of information that can be conveyed in one word is much larger than others, and the content that can be expressed with less code is enriched. It is a level where everything is completed with the first word if you are not good at it.

In other words, it's like ** using a design pattern for class design **.

Only one instance of this class is guaranteed to be created during a system process and is used for caching and sharing information that is common throughout the system.

Rather than saying

This class is a singleton.

It is faster and more accurate to say.

◇ You can put a link from the development document.

And in the javadoc see tag Wikipedia-singleton pattern Or something like that, just skip the reference.

This will save you the trouble of writing a long javadoc yourself, and will allow you to capture more general and open information.

◇ Keywords are easy to go through.

Also, if it's a ** keyword **, it's almost certainly ** a language spec exists **, so the information (perhaps a standard document) will be hit immediately. Or, even if the standard documents do not hit, there is a high possibility that the introductory site (or blog, Qiita article, etc.) that explains them will hit, and there is an advantage that the searchability is high.

In my case, I often use the keyword that came from C # for naming, but in this case ** "If you google with this keyword and MSDN hits, it's the correct answer, so read it carefully !!" ** Fill in that with ** normal comments **.

  • (Since it is a Java development project for development, I was reluctant to openly link to a document in another language, so I usually use comments as hints for those who are willing to read the code. Or, more directly, you may write the URL of the page you want to read directly. ) *

■ Bonus: The cheat sheet used for naming

If I can't find a good borrower, I'll do my best to give it a nice name. This area takes time, so I want to find it as easily as possible.

By saying that, I often refer to this area.

** Naming dictionary for programmers "Codic": ** https://codic.jp/engine

** Summary of cheat sheets that are useful when you are unsure about naming variable names and function names in programming: ** Starting with the introduction of Codic above, there are many other useful things for naming, please look for your favorite one https://nelog.jp/programming-words

** (*'▽') Thank you very much for your help! ** **

■ Reference materials

If you get lost in the naming of the program, you can search the code on GitHub for reference.

An epoch-making method of searching code on GitHub has been proposed. https://qiita.com/kyoshidajp/items/c5d4f060df636ea20cbb

Summary of English words often used in programming [Updated from time to time]

I have found the strongest of the strongest naming cheat sheet. https://qiita.com/Ted-HM/items/7dde25dcffae4cdc7923

Aiming for better naming

It's a convention about naming, or a guideline. https://qiita.com/takasek/items/693c57dc9ddc6c1eb1ba

Recommended Posts