Whats in a Name: Java naming conventions

Whats in a Name: Java naming conventions
Page content

Imagine a world without names. What would happen if nothing in this world had a name? This article would have been a very different story. Oh sorry, we wouldn’t even be able to tell the story. What about if we had weird names? Then what will aliens think about us? bunch of idiots with weird names? So, everything is in the name, especially when it comes to programming languages.

Programming is a story; every character and scene needs to have a name that properly tells you what it is and what it does. You should be careful while naming things: You don’t want to make your code hard to read or debug. Remember: You are the creator here. You can name anything from babies to stars, trees to mountains, almost anything (almost). Paying little attention to this feature will make your code more readable, easy to debug and most importantly you will feel good about this. Trust me. And you should be able to look at it after months and be able to understand it without any trouble.

Now, let us discuss naming conventions in Java while you sip your tea. We will talk about different aspects of Java programming language like package, class, variable, and functions. This article surely is based on java, but it fits almost every language especially Object-oriented languages. Let just dive into it. But before, Fill your cup of tea, start sipping and learn in your tea break.

Package

Packages groups your classes. It should represent what kind of classes it is holding. They come in a hierarchy, starting with the base package name, it should have your company’s domain name in reverse order, plus your project name without any extra character - all in lowercase letters.

package com.coderstea.healthy;

If the name is being too long you can use underscores. Also, You can add the version name in the end.

com.coderstea.healthy;
com.codertea.healthy.whats_in_a_name; // with underscores
com.codertea.healthy.v2; //versioning
org.srpingframework.*;
com.zaxxer.hikari;

Class

The class name should be a noun. Use CamelCase, but the first letter should be capitalized, like HashMap. It should tell you what kind of function and/or variable you can expect from it. It must be the Specialized (explained in Interface).

class ArrayList {}
class HashMap {}
class ComputerEngineer {} //specialized term

Interface

Interface names should be an adjective. Sometimes, it can be a noun like a List or Map. Like classes, it should be in CamelCase, with the first letter capitalized. It must be Generalized, for example, ComputerEngineer. If it were to implement any interface, which one would it implement? Engineer, right? So, the Engineer is the general term, but the ComputerEngineer is the specialized one.

interface Student {} //noun
interface Engineer {} //generalized term
interface Account {}
interface Cloneable {} //adjective

Variable or Fields

Variables or fields should be nouns, short, meaningful, represent the value and usage. They should be written in camelCase. And do not start them with an underscore(‘_’) or dollar sign ‘$’ characters. Additionally, do not write one character variable. You can use one character for temporary variables in a loop or something similar. Commonly used loop characters are i, j, k, m, and n for integers and c for characters, it’s up to you; it’s only temporary. it is even better if you can attach the unit as well, like timeInDays or weightInKilos. For a boolean value, make it more of a question with a yes or no answer. Generally, ‘is’ or ‘has’ is attached at the beginning. You can make numbers more readable by separating them with an underscore.

int countOfCustomer;
float averageInterest;
long timeInMillisecond, daysInYear; //with some unit
boolean isEngineer, isCompleted, hasSubmitted; // questions
int moneyIWishIHad = 1_000_000_000_000;

Constant

Constant is also a big part of the program. You want to make things always stay the way they are. So, the common practice is to make it appear as if they are not going to change, just like a stubborn kid, you have to make them all capital. Yep! No evading of the work here.

static final float PI = 3.14;
static int CREATED_ON_YEAR = 2019;
final String CODERS_TEA_URL = "https://www.coderstea.com";

Function or Method

Who does most of the work here? Methods. They should always report to you what they are trying to do and what action they are taking. So, they should be placed in the verb category and the name should imply what they are doing. Make it 2-3 words long. Then again, use camelCase, as you did in variables. Usually, get and set are used to fetch and set the data to something respectively (setters and getters). For boolean, it’s the same as variables.

void print(Object obj);
void remove(Obejct obj);
Object update();
int getCountOfCustomer();//getter
void setCountOfCustomer(int countOfCustomers);//setter
boolean isUserAdmin(User user);

Generic

There is not much to talk about generics. It is better to use a single capital character for it. Usually, T is used. If more than one the V is also used, Java uses E for the list element, K and V for maps, and S for service loaders. Do not use a multi-character for it. It, then, becomes hard to detect it from another class name.

public <T> void print(T t);
interface List<E>{}
class HashMap<K, V> {};

Remaining Team

There are a few other things, like enum and annotations. They are similar to classes and interfaces respectively. In enum fields, they must all be capital since they are final static by default.

enum Beverage{TEA, COFFEE};
public @interface FunctionalInterface {}
public @interface Autowired{}
enum HttpResponse {Ok, NOT_FOUND....}

Conclusion

Just for the last sip of your tea, we went through not all but few ways to make your code more readable. There are many more of them. It is something I use personally and it made my life easy. Some times it takes so much time to name the things. Heres a trick.

  • Just give 5 seconds and no more than that to decide the name.
  • If the name doesn’t come to you, name it anything so that it will force you to rename it again, like asdfasdfasdfasdf.
  • Finish up your method or class.
  • Then, think about the name again and change it from the temporary, unreadable one to a new one.
  • If it still doesn’t come to you, then divide and conquer. One of the main reasons you may not be able to decide the name is that too many things are going on in a single place. Just make it do one thing at a time. And name that thing along the way.
  • Repeat for all the others.

See you at your next Tea break.