Smooth Java – Part 2.1: Naming – No small story

Tram Ho

Naming anything, for me, has always been a headache. As a coder, renaming is often done, but maybe due to frequent, it is often overlooked by “gurus”. Or even though it is important to know that naming is important, when I get too secret, I tell myself that I should finish writing quickly, but sit out to put each name to do (also true).


  • Choosing good names takes time but saves more than it takes
  • Our goal, as authors, is to make our code as easy as possible to understand. We want our code to be a quick skim, not an intense study.Robert C. Martin – the Author of “Clean Code”

And below I have listed some tips in naming. The tips I give here are mainly related to the meaning of the name. Let’s get started!

1. Every name needs to be sent with something

“If a name requires a comment, then the name does not reveal its intent.”

The simplest example is instead set is

It should be put in its semantic:

“Make your code easy to understand”

For example, you need to design a board game as a 2-dimensional array, where: blocked – marked 1, passed through – marked 0, exit – marked 2.

You need to write a function that updates the player’s position after the move is made.

It looks good but it would be better if you provided more information about what 0 or 2 means.

2. Attached to the nature, avoid misunderstanding

  • Suppose, you need to manage a student comparison of type Student[] , which name is the best match: studentList , students or studentArray . Let’s analyze it:
    • studentList : very confusing that the data type here is List<Student> . So, in my opinion, this name is not appropriate.
    • The other two are suitable. studentArray shows more information but in many cases students are OK

3. Make a true difference

“If names must be different, then they should also mean something different”

This should always be kept in mind as we are not going to put two identical variable names in the same scope. Therefore, we sometimes make them arbitrarily different.

Numbering for variables b1, b2, b3, ... bN often thought of. This is suitable when the objects are really similar. But you try to see the example below:

In fact a1, a2 are not the same and the numbering does not show clearly what the difference is. The solution is to rename the source and target

Next is to ignore the excess words.

For example, if you have a Student class, other names like StudentData or StudentInfo basically make no difference in meaning.

Or, the variable should not appear in the variable name; person should not appear in a person’s name.

So, which NameString should Name and NameString choose? Think there might be a case where Name is floated? If there is NameFloat would be reasonable, if not, just Name is OK.

4. The length of the variable name ~ its scope

Names with an i , a should only be used in short methods such as a local variable.

Conversely, if a variable / constant is used in many places, it should be easy to find and understand

5. More clearly is full of profound implications

“Clarity is king! Professionals use their powers for good and write code that others can understand.”

In loops, the counter is usually named i , j , and k .

So have you ever thought of your counter variable, why not name it c ? It was just as short, but moreover, it was also different.

Smart coders like to make a difference. But your different intelligence to you sometimes becomes confusing for others. So c should only be used if you have used variables a and b .

6. Name and its function

Class Name

First, the name of the class must be a noun, capitalization first letter (general convention)

Avoid words like Manager , Processor , Data , or Info in the name of a class.

There are many instances named SomethingManager , this type of name is very common such as: SessionManager , ConnectionManager , PolicyManager , QueueManager , UrlManager , ConfigurationManager . Sounds fine?

So what do you know about the class named UrlManager ?

This name indicates that this is not a URL, its main job is to work with the URL. But it does not show the specific work of the class (crawl URLs or validate URLs or does it contain all URL manipulation?)

This is the reason why adding Manager to the name is like adding Object, A, An, The . This is a word that is used quite a lot because we often need to create classes to manage and handle objects.

Here I would like to give some suffixes to replace the Manager I found:

  • Bucket/ Pool : used for storing objects that you do not use very often

Example: You need a class to hold connections to a number of different sources. When needed, you will retrieve a connection from that class, and when used, return it. So how is the name okay? – ConnectionBucket

  • Supervisor : for the class whose main job is to allocate work and monitor progress

Example: In a system, the Class responsible for coordinating work for many different users can be named QueueSupervisor

  • Factory : used for the class responsible for creating the object
  • Shepherd : for a class that manages the life cycle of an object (when to create, when to destroy)
  • Synchronizer : used for a class that specializes in copying data between 2 / more objects
  • Helper : used for a class that is only meant to contain methods. For example you have a class PriceHelper , this class will have calculation / processing functions related to price for example ( .calculatePrice(...) )

There are also many other suffixes that can be used such as: Coordinator, Builder, Writer, Reader, Handler, Container, Protocol, Converter, Controller, View, Entity,...

If you know anything more about situations using the above suffixes, please comment!

Very willing to accept: v

Method Name

The method name usually starts with a verb (by convention).

The methods used to access , set , or check a property- related condition should take precedence: get , set , or is

7. Consistency in naming

Pick one word for one abstract concept and stick with it.

For example, the meanings of the three words fetch , retrieve , and get are similar. Using these 3 word combinations can be confusing later on. Therefore, for a purpose / meaning, only one word should be used to describe.

8. Context of the name

You should place names in meaningful context

Sometimes it is necessary to wrap variables into a class if they are usually together

houseNumber you have the following properties: firstName , lastName , street , houseNumber , city , state , and zipcode . It would be better if you had an Address class containing the properties: street , houseNumber , city , state , and zipcode .

Naming is a bit messy, but it doesn’t have to be all right right away. If you come up with a name that is more suitable than the one you have given, then just change.

And important: Don’t get too headache about having the right name in the first place. If you can’t think of it, just let it go. It can be booked later!

Share the news now

Source : Viblo