Why Some Java Variable Naming Styles Are Not Recommended?

Java has specific naming conventions - styles like lowercase names, underscores, abbreviations, and numeric suffixes reduce code clarity.
On this page

Why Some Java Variable Naming Styles Are Not Recommended?

Excerpt

Some common Java variable naming styles like all lowercase, underscores, short names and numeric suffixes are not ideal as they violate conventions and reduce code readability.


Variable naming is an important part of writing clean, readable code in any programming language. In Java, there are specific naming conventions and styles that are recommended for variables and other identifiers. Some common styles, however, are not considered good practice as they can reduce code clarity and understandability. In this article, we’ll look at these not-recommended naming styles and why it’s better to follow standard Java conventions.

Introduction

Variable names in Java should be descriptive and help convey the meaning and purpose of the variable. Styles like camelCase and avoiding underscores are recommended in the standard Java coding conventions. However, you’ll sometimes see variables named with styles like all lowercase, underscores, short abbreviations or numeric suffixes. These styles can cause readability issues though, so should generally be avoided.

Here are some variable naming styles that are not recommended in Java:

Starting with Lowercase

Variable names starting with a lowercase letter go against the standard Java naming conventions. For example:

1int myVar;
2String someVariable;

These should be avoided even though they may seem fine at first glance.

Using Underscores

While underscores are valid in Java variable names, they are somewhat discouraged as they can reduce readability:

1int my_var;
2String some_variable;

The eye has to pause slightly when reading underscore-separated words.

Short or Abbreviated Names

Overly short or abbreviated names don’t convey the true meaning of the variable:

1int var1;
2int tmp;

These names require referring to other code or documentation to understand their purpose.

Names with Numeric Suffixes

Using numeric suffixes like var1, var2, var3 might seem handy but these names areindistinguishable and easily confused:

1String var1;
2String var2;

It’s not clear at a glance what the difference between them is.

Problems with These Styles

Some issues caused by the above naming styles:

  • Violate Java naming conventions - This could confuse other Java developers and reduces standardization.

  • Reduce readability - The names don’t convey clear meaning which impacts understandability.

  • Increase cognitive load - Trying to decipher ambiguous names takes more mental effort.

  • Create maintainability issues - Poor names make code harder to maintain and build upon over time.

Instead of the above styles, these are the recommended naming conventions to follow:

camelCase

The camelCase (or lowerCamelCase) convention capitalizes each word except the first:

1int studentGrade;
2String lastName;

Convert text to camelCase online.

Descriptive Names

Use longer, descriptive names rather than short abbreviations:

1int studentCount;
2int maximumStudents;

Aim for clarity over brevity.

Recommended naming styles like camelCase have several advantages:

  • Follow Java standard coding conventions - More consistent with other Java code.

  • Improve readability - The meaning is clearer at first glance.

  • Reduce cognitive load - Easier to understand without extra mental effort.

  • Enhance maintainability - Built-in meaning makes code easier to maintain.

Examples of Good and Bad Names

Here are examples of good and bad variable names:

Good Names

1int studentGrade;
2double totalSales;

Bad Names

1int Var1;
2int _tmp;

You can use online case conversion tools to check your names match Java conventions.

Conclusion

Certain variable naming styles like underscore-separated or short abbreviations are not recommended in Java as they violate naming conventions and reduce code clarity. Instead, styles like camelCase and longer descriptive names are preferred. This helps improve readability, maintainability and adherence to coding standards. Following Java’s naming best practices leads to higher quality, more professional code.