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
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 “$”.
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:
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.
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:
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.
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.
This was originally published on Dec 18, 2015.