Skip to main content
Engineering LibreTexts

4.2: Identifier Names

  • Page ID
    10255
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Overview

    Within programming a variety of items are given descriptive names to make the code more meaningful to us as humans. These names are called "Identifier Names". Constants, variables, type definitions, functions, etc. when declared or defined are identified by a name. These names follow a set of rules that are imposed by:

    • the language's technical limitations
    • good programming practices
    • common industry standards for the language

    Technical to Language

    • Use only allowable characters (for C++ the first character alphabetic or underscore, can continue with alphanumeric or underscore)
    • Can't use reserved words
    • Length limit

    These attributes vary from one programming language to another. The allowable characters and reserved words will be different. The length limit refers to how many characters are allowed in an identifier name and often is compiler dependent and may vary from compiler to compiler for the same language. However, all programming languages have these three technical rules.

    Good Programming Techniques

    • Meaningful
    • Be case consistent

    Meaningful identifier names make your code easier for another to understand. After all what does "p" mean? Is it pi, price, pennies, etc. Thus do not use cryptic (look it up in the dictionary) identifier names.

    Some programming languages treat upper and lower case letters used in identifier names as the same. Thus: pig and Pig are treated as the same identifier name. Unknown to you the programmer, the compiler usually forces all identifier names to upper case. Thus: pig and Pig both get changed to PIG. However not all programming languages act this way. Some will treat upper and lower case letters as being different things. Thus: pig and Pig are two different identifier names. If you declare it as pig and then reference it in your code later as Pig – you get a compiler error. To avoid the problem altogether, we teach students to be case consistent. Use an identifier name only once and spell it (upper and lower case) the same way within your program.

    Industry Rules

    • Do not start with underscore (used for technical programming)
    • variables in all lower case
    • CONSTANTS IN ALL UPPER CASE

    These rules are decided by the industry (those who are using the programming language). The above rules were commonly used within the "C" programming language and have to large degree carried over to C++.

    Definitions

    reserved word
    Words that cannot be used by the programmer as identifier names because they already have a specific meaning within the programming language.

    This page titled 4.2: Identifier Names is shared under a CC BY license and was authored, remixed, and/or curated by Kenneth Leroy Busbee (OpenStax CNX) .

    • Was this article helpful?