Naming Variables

There are a few things you have to consider when naming variables. The first and foremost thing is that they must be legal, the compiler will not let you go on with an illegal variable name. What is legal, you ask? Well, here are the ground rules:

What does this all mean? The first item is pretty self explanatory. Use a letter as the first character in your variable name. Good style is to NOT use the underscore character (_) as the first character (there is an underlying reason behind this, but do not worry about it now, just don't do it).

The second item states that it must contain at least one letter - being it must start with a letter. If you use only one character, it absolutely must be a letter. Why would you do this, use only one letter for a variable? You'll see in the next section. After the first letter, any alpha-numeric character is legal.

It cannot be a reserved word. What is a reserved word? It is a word that the compiler already has a predefined use for, such as int or float. There are several reserved words, and the easiest way to tell if a word is reserved is to watch as you type it into the BeIDE. If it is a reserved word, it will change color. Here are some of the keywords you cannot use:

This is by no means all of them, but it is some that you are likely to run into sooner rather than later. One thing to note: because C++ is case-sensitive, it is possible to name a variable "While" and it would not be recognized by the compiler as "while". This, however, is very, very BAD style, and is definitely, very highly, not at all recommended.

Now, this maximum size thing. Some compilers on certain platforms (mostly much older ones) will limit you to 8 or 16 characters. Newer compilers, BeIDE included, will allow more characters than you are likely to ever use, to the point of being unreadable and in "bad taste". So this is really a non-issue these days, unless you happen to want to port your program over to MS-DOS 2.0 using an early version of Turbo C++ or something.

Okay, legalities aside, the second thing you must consider is style. We have already discussed naming your variables as modified keywords ("While") and naming them something too long to readily read. But what else? There are many things you can name a variable that are legal that would be considered "bad style". Here are a few examples of "bad style":

Now some legal, good examples that are easy to read and understand:

You'll notice if you use the longer variable name with these styles, it is much easier to read:

Consistency is also very important. Figure out what works for you and stick with it. Another good commenting style is to name all the variables that are being used and what they are being used for in plain english at the beginning of the function:

//someVarThatUNeed - int - used for calculations in the widget equations.

That way, one reading your code can always refer to the top of the function to find out exactly what the varible is for / does.

Hungarian Notation is often used for labeling variables and objects with short, somewhat descriptive identifying names (this is mostly used by MS platform coders for GUI widgets - buttons, labels, textboxes, check boxes, etc., but you may use something similar if it works for you). Here are some examples of this notation:

Datatypes:

GUI Widgets:

Some of these may not even apply to anything you ever do, and there are many more that are not listed here (most apply to the MS platform, though). But you get the idea, if you consistently use some method similar, things get a lot easier to read. Of course, the use of Hungarian Notation or any specific "styles" is completely up to you, but be aware that some coders actually expect some of these conventions, at least the more basic ones.

Now, a short program to demonstrate data types.