Naming Conventions

FIELDS OF STUDY

Coding Techniques; Software Development; Computer Science

ABSTRACT

Naming conventions are systems of rules to use when naming variables, constants, methods, and other components of computer code. When applied consistently, naming conventions make code easier to read, understand, and search. They take little time to apply while coding and save valuable development time when code is reviewed.

PRINCIPAL TERMS

WHAT ARE NAMING CONVENTIONS?

Naming conventions are the rules that programmers are expected to follow when choosing identifiers to represent different data types and structures, such as variables and constants. Different programming languages follow different naming conventions, and companies often have their own in-house conventions as well. Failure to adhere to naming conventions will not prevent a program from compiling or executing, assuming that the name does not violate the syntax of the language used. It will, however, make the program code more difficult for people to read and understand. Following established conventions is especially important when multiple programmers are working on the same project or when the code is likely to be reviewed, changed, or reused in the future.

When designing a naming system, several factors must be considered. Will names be restricted to a fixed length or a minimum or maximum length? What information will each name contain? Names that are very short, such as one-letter identifiers, do not provide enough information. Very long names can be onerous to type repeatedly, which may lead to errors. Modern code-editing software provides autocomplete functions that reduce these types of errors. If these tools are used, some of the drawbacks of using longer names are mitigated. Names should be easy to understand and remember. The naming system must be logical to make it easier to learn, remember, and use.

Naming conventions require clarity. Names should convey enough information to avoid possible confusion with similar objects or methods. Because of this, multiword names are often preferred. If a programmer is writing a procedure that will add up all purchases made in a week, rather than naming the procedure sum(), the programmer might use the name sumWeeklyTotal(), which is clearer and describes the procedure's purpose. Beginning each word in a name with a capital letter, a style known as “camel case,” makes such names more readable. Other methods for distinguishing words in a multiword name include separating the words with hyphens or underscores.

SAMPLE PROBLEM

A retailer needs a method created that will apply a 10 percent discount to purchases over $200. The source code is as follows:




Naming Conventions

Rename the method, the variables, and the constant using the following rules:

Do not use spaces in names.

Methods are represented by a verb-noun pair, such as getDate(), and are set in lower camel case, with all words capitalized except for the first word.

Variables should be easy to read and remember and should be longer than one character. They should also be set in lower camel case.

Constants should be set in all capital letters, and words should be separated by underscores.

Answer:

One possible answer is below.




Naming Conventions

While meaningful identifiers are preferred, redundancy within hierarchical systems, such as classes or tables, should be avoided. In a class called FootballPlayer, for example, a programmer should avoid naming a method footballPlayerRun(), as this name provides redundant information. In this case, the name run() would be preferable.

Naming schemes that include abbreviations, numbers, or codes can be confusing to read and difficult to remember, wasting development time and possibly leading to errors. When abbreviations are used to create names, it is easy to make mistakes because of inconsistent usage. This can result in searches for a particular abbreviation returning incorrect results, making code more difficult to write, debug, and maintain. Naming conventions that require a key to determine codes to use when creating names should be avoided, as they are time consuming and onerous to use and implement. Avoid slang, inside jokes, or humorous names when naming objects.

WHY USE NAMING CONVENTIONS?

Naming conventions make code clearer and easier to read and understand. They also add descriptive information, or metadata, that helps explain each component's purpose. For example, a method can be named using a verb-noun combination that expresses the purpose of the method. A consistent naming convention also allows for more efficient searches and avoids confusion between items in the code.

For naming conventions to be effective, they must be used by all members of a development team in a consistent fashion. If one naming structure is expected and another is found, confusion can arise. In order to encourage consistent use of a naming convention, rules should be clear, and names should be easy to create.

A good naming convention can also help programmers more clearly define the purpose of their own code. If a developer finds it difficult to create a name for a component, they might need to clarify the reason the component is being used. In this way, an effective naming convention can aid in the development of robust, error-free code.

NAMING CONVENTIONS IN ACTION

Naming conventions are important to the ensure clear and consistent code. A naming convention should be simple and easy to remember and use. Properly designed naming conventions make code easier to read without imposing undue burdens on developers.

While naming conventions differ among languages and organizations, and sometimes even among different groups in the same organization, there are certain common elements among them. One such element is the aversion to single-letter identifiers, except sometimes in the case of temporary variables. Another element common to naming conventions that favor delimiting words by camel case is that different types of identifiers follow different capitalization rules. For example, it is a widely used convention in Java that class names should be set in upper camel case, where the first word is capitalized (UpperCamelCase); method and variable names in lower camel case, where the first word is lowercase (lowerCamelCase); and names of constants in all capital letters, with words separated by underscores (ALL_CAPS). Package names should be set all in lowercase, typically in a hierarchical order, with levels separated by periods and multiword level names run together (domain.product.feature or companyname. productname.feature).

WHY ARE NAMING CONVENTIONS IMPORTANT?

Naming conventions may seem secondary to the logic of the code itself, but if the code created cannot be easily understood, it is more difficult to create, debug, and maintain. Naming conventions make it easier for programmers to work with each other's code. They can also help developers understand their own code while writing complex programs over long periods of time. Effective naming conventions reduce the time wasted reinterpreting the same information multiple times. With good naming conventions, code is easily understood and can be explained to less technical collaborators.

—Maura Valentino, MSLIS

Friedman, Daniel P., and Mitchell Wand. Essentials of Programming Languages. 3rd ed., MIT P, 2008.

Haverbeke, Marijn. Eloquent JavaScript: A Modern Introduction to Programming. 2nd ed., No Starch Press, 2015.

“Java Programming Style Guidelines.” GeoSoft, Geotechnical Software Services, Apr. 2015, geosoft.no/development/javastyle.html. Accessed 14 Feb. 2017.

MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.

Schneider, David I. An Introduction to Programming Using Visual Basic. 10th ed., Pearson, 2017.

Scott, Michael L. Programming Language Pragmatics. 4th ed., Elsevier, 2016.

Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, 2004.

Way, Jeffrey. “9 Confusing Naming Conventions for Beginners.” Envato Tuts+, Envato, 22 Oct. 2010, code.tutsplus.com/articles/9-confusing-naming-conventions-for-beginners--net-15584. Accessed 14 Feb. 2017.