avatarLincoln W Daniel

Summary

The provided web content discusses Java programming language naming conventions, emphasizing the importance of clear and consistent coding practices for readability and collaboration.

Abstract

The web content begins by drawing a parallel between learning to write sentences in English and writing code in Java, highlighting the foundational role of naming conventions in both language and programming. It outlines three primary objectives in programming: creating functional software, adhering to language conventions for code clarity, and documenting code for future reference. The article delves into Java naming conventions for variables, classes, and methods, stressing the use of camel case for variables and methods, and upper camel case for classes. It also distinguishes between variable naming for constants, which should be in all caps with underscores, and regular variables, which should avoid the use of underscores and dollar signs. The text underscores the significance of these conventions in facilitating understanding and collaboration among developers, and it provides examples and references to further reading on the subject.

Opinions

  • The author believes that adhering to Java naming conventions is crucial for writing code that is understandable and maintainable by others.
  • The use of descriptive and self-documenting variable names is advocated to enhance code readability.
  • The article suggests that using standard conventions, such as camel case and all caps for constants, helps differentiate between mutable and immutable variables.
  • The author values the clarity that comes from following established programming practices and sees it as a way to effectively communicate intent through code.
  • There is an emphasis on the importance of learning and internalizing Java conventions early on in the programming learning process.
  • The author implies that following naming conventions is not just a matter of preference but a key component of professional software development.

Remember when you were in the first grade and you had to write your first sentence? The teacher said your sentences must include a subject and a verb. Earlier, she made sure to teach everyone what a subject is and what a verb is so that she could later use it to communicate new ideas, like sentences, with confidence that everyone would understand her. At this point, she was able to tell you how to structure your sentences:

“All sentences must start with a capital letter and end with a punctuation.”

She continued to explain the different types of sentences you can write along with each one’s appropriate punctuation:

“Statements end with a period and questions end with a question mark. Sometimes, we get excited when we say things, so we can use an exclamation point to show how excited we are!”

That is a great teacher. She taught you the conventions of speaking English so that you could effectively communicate your thoughts with your classmates, parents, and the rest of the world in a way everyone who knows English can understand. We do the same thing in Java and every other programming language so others can understand our code.

Why Naming Conventions are Important

When programming, you have three goals:

  • Design and build software that work
  • Follow the conventions of the language, Java, so others can understand your code and work with you
  • Document your code through comments so that you and others can understand and manipulate it later

Before moving forward, it’s important that we take some time to learn the conventions of Java. By doing this, we will be able to design and build better software with code that others can understand and collaborate.

While there are a number of conventions to follow, we will focus on the ones that are most important for beginners to grasp.

Java Naming Conventions

Variable Naming

String author = "Lincoln W Daniel";
String bookTitle = "Java for Humans";
int yearBookPublished = 2016;

What’s Allowed

Variables in Java are case sensitive and can be of unlimited sequence of letters and numbers. However, variable names must start with a letter, underscore character “_”, or a dollar sign “$”.

The Convention

While you can start with a underscore or dollar sign, the convention for naming variables in Java is to always start with a lowercase letter. To help make your code self-documenting, you should use full words to name a variable instead of single letter; variables named day, month, and year, for example, are more intuitive and readable than their abbreviated versions d, m, and y respectively.

If your variable name consists of more than one word, start the first word with a lowercase, but capitalize each subsequent word. This is called camel case in reference to a camel’s back; variables use lower camel case because the first letter of the variable name is lowercase. Take the variables nameOfParent1 and nameOfParent2 which hold the names of your first parent, perhaps mom, and the name of your second parent, dad. Those variables consist of three words and a number written in lower camel case.

Another convention is to never use the dollar sign in your variable names. It’s better to stick with words and numbers for your variable names. The underscore character is also discouraged in most cases of variable naming. Things get a little different for naming constant variables, though.

The previous variable naming conventions apply for variables that will likely change their values during execution of the program, your code. However, when you need a variable that holds a constant value, you should use all capital letters with underscores separating each word. This way, it is clear to you and everyone who reads your code which variables can change and which can’t. If we want to write a calendar program, we will have a couple variables that change values, the current day and month, and a couple that are constant, the number of days in a year:

static final double NUMBER_OF_DAYS_IN_YEAR = 365.25;
static final int NUMBER_OF_WEEKS_IN_YEAR = 52;
String currentDay = "Tuesday";
String currentMonth = "May";
int currentWeek = 24;

The static keyword will be explained later, but it’s important to know that the final keyword means the variable’s value cannot change after it has been set: we will always have 365.25 days in a year. Such a variable is often called a constant because it cannot be changed once its set.

One last convention for now is your variables cannot match keywords that are reserved for Java such as int, double, for, while, and others that can be viewed here. But don’t worry, Java won’t let you make that mistake.

Class Naming

The Convention

We haven’t talked much about classes, but I will go over them more later. For now, just know that when naming classes, you should use whole words like in variables. However, class names should employ upper camel case: the first letter of every word in the name of a class should be capitalized. Take a class that holds information and functions of a human for example. We should name such a class Human because that is descriptive of what the class is for and what kind of objects can be created from it. Another class may hold information and functions of a game character, say a fire archer; we should name it FireArcher:

public class FireArcher {...}

Method Naming

The Convention

Method names, like variables, should use lower camel case with a mix of words and numbers. Further, a method’s name should consist of verbs that say what it does.

public int add2Numbers(int firstNumber, int secondNumber) {
    int sum = firstNumber + secondNumber;
    return sum;
}

Anyone who knows Java will be able to tell that your method, add2Numbers, takes two integers, adds them together, and returns the sum.

That’s it for now. We will learn more conventions as we learn more about Java. Follow all of these conventions and you will write code that everyone wants to work with because they will be able to understand and help you.

Next Chapter

Table of Contents

A product of JavaForHumans.com by Lincoln W Daniel, the ModernNerd.

Programming
Java
Coding
Recommended from ReadMedium