User Tools

Site Tools


guides:programstyle:identifiers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
guides:programstyle:identifiers [2020/07/24 11:00] – created wikiadminguides:programstyle:identifiers [2022/08/02 11:59] (current) – external edit 127.0.0.1
Line 2: Line 2:
  
 In a programming language, and **identifier** is a name associated with a user declared item in the program.  This may include variables, constants, types, functions, or other items. In a programming language, and **identifier** is a name associated with a user declared item in the program.  This may include variables, constants, types, functions, or other items.
 +
 +There are two basic conventions that simplify program comprehension when creating identifiers.  Identifiers name should be meaningful and should follow the formatting convention.
 +
 +===== Identifier Formatting Convention =====
 +
 +By correctly formatting an identifier, readers of the program are able to instantly determine the type of identifier they have encountered.  
 +
 +==== Variables ====
 +
 +Variables names should begin with a lower case letter.  If the name contains more than one word, each additional word should begin with an upper case letter. This is known as //camel case//.
 +
 +Examples of acceptable use:
 +<code c++>
 +int price;
 +float costToConsumer;
 +double numberOfCars;
 +</code>
 +
 +Examples of unacceptable use:
 +<code c++>
 +int size_of_house;
 +double PAY_AMOUNT;
 +</code>
 +
 +==== Named Constants ====
 +
 +Named constants should be declared in all upper case letters, with the underscore seperating multiple words.
 +
 +Examples of acceptable use:
 +<code c++>
 +const PI = 3.14149;
 +const size_t MAX_ARRAY_SIZE = 20;
 +const int NUMBER_OF_PEOPLE = 100;
 +const string INPUT_FILE_NAME = "students.dat";
 +</code>
 +
 +Examples of unacceptable use:
 +<code c++>
 +const float Pi = 3.14;
 +const string fileName = "hello.txt";
 +const int NUMBEROFSTARS = 2000;
 +</code>
 +==== Functions ====
 +
 +Each word in a function name begins with an upper case letter.  This is sometimes called camel caps.
 +
 +Examples of acceptable use:
 +<code c++>
 +void Uppercase(string & word);
 +int NumberOfLetters(string word);
 +</code>
 +
 +Examples of unacceptable use:
 +<code c++>
 +string userinput();
 +</code>
 +==== User Defined Types ====
 +
 +When declaring user defined types use camel caps but end the name with a T.
 +
 +Examples of acceptable use:
 +<code c++>
 +class SpeciesT {
 +...
 +};
 +
 +typedef unsigned char MonthT;
 +</code>
 +
 +Examples of unacceptable use:
 +<code c++>
 +struct Person{
 +...
 +};
 +</code>
 +
 +===== Meaningful Identifier Names =====
 +
 +One key to self documenting code is to make identifier names that makes the code more readable without comments.  While //meaningful// is somewhat open to interpretation, programmers should strive to produce c identifier names that support clear interpretation of the code.
 +
 +Examples of acceptable use:
 +<code c++>
 +int sizeOfInput;
 +double totalWeight;
 +</code>
 +
 +Examples of unacceptable use:
 +<code c++>
 +int a;
 +float rotAng;
 +</code>
 +
 +One exception to this rule is for loop control variables which can be single letters, normally //i//, //j// ....
 +
 +
 +
 +
  
guides/programstyle/identifiers.1595588439.txt.gz · Last modified: 2022/08/02 11:59 (external edit)