Home Best Practice Java Naming Conventions: What’s In a Name?

Java Naming Conventions: What’s In a Name?

Published: Last Updated on 0 comment

Hey, Tea Lovers! Today we will talk about the java naming conventions, according to the best practices.

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, that we are a bunch of idiots with weird names? So, everything is in the name, especially when it comes to programming languages. So it is important to focus on naming conventions.

Before starting this post, I want to tell you that it is just a fraction of what you can do to increase the readability of your code. For more in-depth knowledge you can read Clean Code by Uncle Bob (affiliate link). This book has helped millions of developers including me to see more of your code. Hope this will help you too.

Programming is a Story

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 because 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, and 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 Java naming conventions while you sip your tea. We will talk about different aspects of Java programming language like packages, classes, variables, and functions. This article is based on java, but it fits almost every language, especially Object-oriented languages. So you can apply it to any other language.

Let’s just dive into it and let see the Java Naming Conventions. But before, fill your cup of tea, start sipping, and learn in your tea break.


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Java Package Naming Conventions

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 characters – all in lowercase letters.

package com.coderstea.healthy;Code language: CSS (css)

If the name is too long you can use underscores. You can also add the version name at the end.

com.coderstea.healthy;com.codertea.healthy.whats_in_a_name; //with underscores
com.codertea.healthy.v2; //versioning
org.srpingframework.*;
com.zaxxer.hikari;Code language: JavaScript (javascript)

Java Class Naming Conventions

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 Specialized (explained in Interface).

class ArrayList {}
class HashMap {}
class ComputerEngineer {} /*specialized term*/Code language: JavaScript (javascript)

Java Interface Naming Conventions

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, Engineer is the general term, but the ComputerEngineer is the specialized one.

interface Student {} /*noun*/
interface Engineer {} /*generalized term*/
interface Account {}
interface Cloneable {} /*adjective*/Code language: PHP (php)

Variable or Fields Naming Conventions

Variables or fields should be nouns, short, meaningful, and 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, 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 */
long moneyIWishIHad = 1_000_000_000_000;Code language: JavaScript (javascript)

Constant of Java

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://coderstea.in";Code language: PHP (php)

Function or Method Naming

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 a 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);Code language: JavaScript (javascript)

Java Generics

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 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> {};Code language: PHP (php)

Other Java Naming Conventions

There are a few other things, like enums 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}Code language: PHP (php)

A Trick to Name Something In Java

Sometimes it takes so much time to name the things. Most of the time it doesn’t quickly to mind. In such cases, you can use this 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 a 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 on 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.

Conclusion

Just for the last sip of your tea, we went through not all but a 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.

You can look at the examples of CodersTea on GitHub, GitLab, and BitBucket. I have written multiple best practices posts such as “How to Achieve Greatness in JDBC Performance”, “Recycle the Threads and Save the Resources with Thread Pool”, and many more in the “Best Practices” category.

See you in the next post. HAKUNA MATATA !!!


I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.


Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Ads
Ads
Ads

@2023 All Right Reserved. Designed and Developed by CodersTea

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More