Free Hosting

Keywords and naming identifiers

Keywords and naming identifiers in c++

Keywords
C++ reserves a set of 63 words for it’s own use. These words are calledkeywords, and each of these keywords has a special meaning with in the C++ language. The 15 keywords that are starred (*) were added to the language after it’s initial release, consequently, some older reference books or material may omit these.
Here is a list of all the C++ keywords:
asm
auto
bool *
break
case
catch
char
class
const
const_cast *
continue
default
delete
do
double
dynamic_cast *
else
enum
explicit *
export *
extern
false *
float
for
friend
goto
if
inline
int
long
mutable *
namespace *
new
operator
private
protected
public
register
reinterpret_cast *
return
short
signed
sizeof
static
static_cast *
struct
switch
template
this
throw
true *
try
typedef
typeid *
typename *
union
unsigned
using *
virtual
void
volatile
wchar_t *
while
You have already run across some of these keywords, including intvoidreturnusing, and namespace. Along with a set of operators, these keywords define the entire language of C++ (preprocessor commands excluded). Because these keywords have special meaning, your IDEs will change the text color of these words (usually to blue) to make them more visible.
By the time you are done with this tutorial, you will understand what almost all of these words do!
Identifiers, and naming them
The name of a variable, function, class, or other entity in C++ is called an identifier. C++ gives you a lot of flexibility to name identifiers as you wish. However, there are a few rules that must be followed when naming identifiers:
  • The identifier can not be a keyword. Keywords are reserved.
  • The identifier can only be composed of letters, numbers, and the underscore character. That means the name can not contains symbols (except the underscore) nor whitespace.
  • The identifier must begin with a letter or an underscore. It can not start with a number.
  • C++ distinguishes between lower and upper case letters. nvalue is different than nValue is different than NVALUE.
Not too difficult, eh?
Now that you know how you can name a variable, let’s talk about how you should name a variable.
First, it is a convention that variable (and function) names begin with a lower case letter. If the variable or function name is one word, the whole thing should be written in lower case letters.
1
2
3
4
5
int value; // correct
int Value; // incorrect (should start with lower case letter)
int VALUE; // incorrect (should start with lower case letter)
int VaLuE; // incorrect (see your psychiatrist) ;)
If the identifier is a multiword name, there are two common conventions: separated by underscores, or intercapped.
1
2
3
4
5
int my_variable_name; // correct (separated by underscores)
int myVariableName; // correct (intercapped)
int my variable name; // incorrect (spaces not allowed)
int MyVariableName; // incorrect (should start with lower case letter)
In this tutorial, we will use the intercapped approach because it’s easier to read (it’s easy to mistake an underscore for a space in dense blocks of code).
Second, and this is perhaps the most important rule of all, give your identifiers names that actually describe what they are. It is typical for inexperienced programmers to make variable names as short as possible, either to save on typing or because they figure the meaning is obvious. This is almost always a mistake. Ideally, variables should be named in a way that would help someone who has no idea what your code does be able to figure it out as quickly as possible. In 3 months, when you look at your program again, you’ll have forgotten how it works, and you’ll thank yourself for picking variable names that make sense. The more complex the code the variable is being used in, the better name it needs to have.
int ccountBadNobody knows what a ccount is
int customerCountGoodClear what we’re counting
int iBadWhat does I stand for?*
int indexGoodThis variable is indexing something
int _countBadDo not start variable names with underscore
int countEitherOkay only if obvious what we’re counting
int dataBadWhat kind of data?
int value1, value2EitherCan be hard to differentiate between the two
int numberOfApplesGoodDescriptive
int totalScoreGoodDescriptive
int monstersKilledGoodDescriptive
int x, yEitherOkay only in trivial mathematical functions
* Note: it is okay to use trivial variable names for variables that have a trivial use, such as loop variables. We will address this topic in the section on flow control.
Third, a clarifying comment can go a long way. For example, say we’ve declared a variable named numberOfChars that is supposed to store the number of characters in a piece of text. Does the text “Hello World!” have 10, 11, or 12 characters? It depends on whether we’re including whitespace or punctuation. Rather than naming the variablenumberOfCharsIncludingWhitespaceAndPunctuation, which is rather lengthy, a well placed comment on the declaration line should help the user figure it out:
1
2
// holds number of chars in a piece of text -- including whitespace and punctuation!
int numberOfChars;
Hungarian notation
One method that is sometimes used in naming variables is called Hungarian notation. In Hungarian notation, variables always begin with a prefix that indicates the variable’s type.
1
2
3
4
5
int nValue; // the n before Value represents that this is an integer
bool bValue; // b means boolean
char chValue; // ch means char
double dValue; // d means double
float fValue; // f means float
We will talk more about Hungarian notation later in this section once we’ve covered the different numerical data types.
Quiz
Pick which variables are improperly declared and named according to standard conventions (ie. how you should name a variable).
1) int sum;
2) int _apples;
3) int cats=5, dogs=5;
4) int VALUE;
5) int clouds=5, int trees;
6) int my variable name;
7) int length=3; int width=4;
8) int TotalCustomers;
9) int void = 5;
10) int nAngle;
11) int 3some;
12) int meters_of_pipe;
13) int length, width=5;

0 comments:

Blogger Template by Clairvo