Top Ten Tips for Correct C++ Coding
My first introduction to the C family of languages was decades ago (yes, I know this dates me horribly). Later I learned C++. I wish someone back then had steered me around the most obvious potholes; it might have saved me hundreds of frustrating hours.
I can give you at least a few of those much-needed (excuse the term) pointers. This isn’t a tutorial on C++ but a guide for those in the middle of learning it. But let’s face itwith C++, there’s always something to learn.
What follows are 10 of the more important things to keep in mind if you want to write polished, professional C++ code that is easy to maintain and less likely to need debuggingso I am actually more exacting here in this article than I sometimes am in my book, C++ Without Fear, Second Edition.
These guidelines are in no particular order (sorry David Letterman), except that the earlier items are addressed more to mistakes that beginners have trouble with.
#1: Don’t Confuse Assign (=) with Test-for-Equality (==).
This one is elementary, although it might have baffled Sherlock Holmes. The following looks innocent and would compile and run just fine if C++ were more like BASIC:
if (a = b) cout << "a is equal to b.";
Because this looks so innocent, it creates logic errors requiring hours to track down within a large program unless you’re on the lookout for it. (So when a program requires debugging, this is the first thing I look for.) In C and C++, the following is not a test for equality:
a = b
What this does, of course, is assign the value of b to a and then evaluate to the value assigned.
The problem is that a = b does not generally evaluate to a reasonable true/false conditionwith one major exception I’ll mention later. But in C and C++, any numeric value can be used as a condition for “if” or “while.
Assume that a and b are set to 0. The effect of the previously-shown if statement is to place the value of b into a; then the expression a = b evaluates to 0. The value 0 equates to false. Consequently, a and b are equal, but exactly the wrong thing gets printed:
if (a = b) // THIS ENSURES a AND b ARE EQUAL... cout << "a and b are equal."; else cout << "a and b are not equal."; // BUT THIS GETS PRINTED!
The solution, of course, is to use test-for-equality when that’s what you want. Note the use of double equal signs (==). This is correct inside a condition.
// CORRECT VERSION: if (a == b) cout << "a and b are equal.";
#2: Do Get Rid of “Magic Numbers”
Magic number refers to a literal number that pops up in a program without explanation. Most seasoned programmers prefer to see a program consisting of nothing but symbolic names like MAX_ROWS, SCREEN_WIDTH, and so on.
In short, professional computer programmerssome of the most mathematically inclined people aroundreally hate numbers!
A bit of history helps explains why. Back in the 1940s, all programming was in machine code, consisting of nothing but raw bit patterns. Programmers lived in the lowest circle of hell, having to constantly translate these patterns. Programming became a thousand times easier when assembly language enabled the use of symbolic names.
And even today, programmers tend to dislike declarations like these:
char input_string[81];
81 is a “magic number.” Where does it come from? The better policy is to control the use of numbers with #define or enum statements.
#define SCREEN_WIDTH 80
SCREEN_WIDTH is more meaningful than 81, and if you decide to reset this width later, you need change only one line in the program. That change is then automatically reflected in statements such as:
int input_string[SCREEN_WIDTH + 1];
#3: Don’t Rely on Integer Division (Unless That’s What You Want)
The integer type (int in the C/C++ family, along with short, long, and now long long) is to be preferred whenever you don’t need to store fractional quantitiesfor a lot of good reasons, which I won’t go into here.
But sometimes, integer quantities are part of larger expressions that do involve fractions. Here’s an example from my boo, C++ Without Fear:
cout << results / (n / 10);
The program generates random numbers between 0 and 9; each should come up about 1/10th of the time. The total “hits” for each number is then compared to the expected total, N/10. So if the results (total hits) for the number 3 is, say, 997, and there are 10,000 trials, the number 997 is compared to the expected hitsin this case, 1000.
But results, n, and 10 are all integers. So 997 is divided by 1000, producing[el] zero!
Wait a moment. We were supposed to get 0.997. What went wrong?
Integer division rounds down, producing a nice integer result. The remainder is thrown away. This isn’t necessarily as bad as it seems. C++ provides two separate operations: division and remainder division:
int dividend = n / m; // Put ratio here. int remainder = n % m; // Put remainder here.
By the way, the previous code fragment example used ashudder!magic number, 10. But the next section addresses the bigger problem: loss of data.
#4: Do Use Data Promotion to Control Results
In mixed integer/floating-point expressions, integer operands are promoted to type double. Consequently, this expression produces what we want:
cout << results / (n / 10.0);
Note that 10.0, though having no fraction, is stored as type double; this causes C++ to promote n and results to type double as well, and then carries out floating-point division.
Other ways to produce this effect include use of data casts:
cout << results / (n / (double) 10); cout << results / (n / static_cast<double>(10));
#5: Don’t Use Non-Boolean Conditions (Except with Care)
Designed originally to help write operating systems, the C language was meant to give programmers freedomnot only to manipulate data at a machine-code level (through the use of pointers) but also to write shortcuts. Shortcuts are dangerous for beginners but sometimes nice for programmers who know what they’re doing. If they’re willing to live dangerously.
One of the most elegant tricks is the following, a slick way of saying “Do something N times:”
while (n--) { do_something(); }
You can make this even shorter:
while (n--) do_something();
Again, we’re dealing with the rule that any numeric value can be a condition in C and C++. When n becomes 0, after being decremented once each trip through the loop, the loop ends. But there are problems: What if n contains an initial negative value? Then the loop I’ve just shown goes on forever, or at least goes to the lowest negative value before overflowing. That can really ruin your day.
In general, then, only expressions that are strict Boolean (that is, true/false) expressions should be used as conditions.
while (n-- > 0) { do_something(); }
There’s one major exception. The shortcut makes sense when dealing with a pointer set to NULL (that is, 0) when an operation fails. Effectively, NULL means false. A null pointer can also be used in the context of a linked list to indicate a pointer that is at the end of the list, with its next_node member pointing nowhere (Nowheresville, they said in the 1950s and 1960s). In the following case, a null pointer means a file-open failed:
if (! file_pointer) { cout << "File open failed."; return ERROR_CODE; }
By the way, this is one place where you might want to use an assignment inside a condition:
if (! (file_pointer = open_the_file(args))) { cout << "File open failed."; return ERROR_CODE; }
#6: Do Use using Statement, Especially with Smaller Programs
Technically, common data-stream objects cin and cout are members of the std namespace, requiring you to write code like this:
std::cout << "Hello." << std::endl;
What a load of extra work! (And what a load.) For most programming, I strongly recommend bringing in the entire std namespace, thus eliminating the need to use the std:: prefix with cin, cout, etc. Just place the following at the beginning of every program:
using namespace std;
#7: Don’t Use Global Variables Except to Communicate Between Functions
Only a few words need be said about this one. Programmers sometimes ask themselves whether to make a variable local or global. The rule is simple: If a variable is used to store information that communicates between functions, either make the variable into a parameter (part of an argument list), passing the information explicitly, or make it global.
When information is to be shared between several functions, it’s often most practical to just go with global variables. In syntactic terms, this means declaring them outside all function definitions.
The reason for using relatively few variables global is clear: With global variables, the internal actions of one function can interfere with the internal actions of other functionsoften unintentionally, especially in a large program. So try to make most variables local if you can. Enough said.
#8: Do Use Local Variables with the for Statement
With all but the most ancient versions of C++, the slickest way to localize a variable is to declare a loop variable inside of a for statement. This ability was not always supported, so old-timers like me sometimes need reminding that you can do this.
for (int i = 1; i <= n; i++) { // Declare i inside the loop. cout << i << " "; // Print out numbers 1 to n. }
In the case of for loops, it’s rare that the final value of i, the loop variable, will be used later in the program (although that does happen occasionally). Generally speaking, you’re going to want to use a variable such as i for a loop counter and then throw it away when you’re done. Making i local to the loop itself is not only a slick way to save some space, but it’s safer programming.
One of the benefits of this use of for is that it automatically takes care of having to initialize i, a local variable. Yes, it can be extra work, but initializing a local variable is something you ideally ought to doalthough it is tempting to leave it uninitialized if the very first statement in the function is going to set the variable’s value. Nonetheless, the most correct programs tend to initialize their locals:
void do_stuff() { int n = 0; // Number of repetitions int i = 0; // Loop counter.
Remember that initialized global variables (including objects) contain all-zero values, while uninitialized locals (including objects) contain garbagegarbage being a technical term for “meaningless, useless value that is likely to blow up your program.”
#9: Don’t Be Intimidated into Overusing Classes and Objects
This is a general philosophical pointer. When people start programming in C++, they’re typically told to make every data structure into a class and to make every function a class member.
But object-oriented programming (OOP), which was so hyped in the 1990s and in the early years of the millennium, has suffered a bit of a backlash in the last few years. The truth is, for short console-output-oriented programs, it’s nearly impossible to find examples in which classes and objects save programming effort. On the contrary, programs that unnecessarily use classes take up more space.
Why use classes at all, then? Consider that object-oriented concepts were pioneered by the same people who came up with graphical-user interface (GUI)[el] no, not Microsoft, and not Apple, despite its claims. The originator of both these technologies was PARC, the Palo Alto Research Center.
It’s not surprising, then, that classes and objects provide best and highest use within a graphical or event-driven system. An objector instance of a classis a self-contained data item with both state and behavior, meaning it knows how to respond to requests for services. This fits the GUI model very well.
Beyond that, I recommend mastering the basics of class and object syntax so you can take advantage of the Standard Template Library (STL). The STL provides a lot of rich facilities, including strings, lists, and stacks, that simplify a lot of programming tasks.
#10: Do Remember: Semi’s After Class Declarations, Not Functions
Once you do start defining classes, don’t get tripped up by the syntax.
BASIC programmers sometimes complain, “I don’t want to have to think about how often to use a semicolon goes.” This made them dislike Pascal. C and C++ are at least a little better; the semicolon (;) is not a statement separator but a statement terminator, making its use more consistent.
But you don’t terminate a compound statement with a semi:
if (a == b) { cout << "a and b are equal."; }
So the rule is to terminate each statement with a semicolon, but don’t follow a closing brace with a semicolon. Consequently, function definitions are not semicolon-terminated.
There is one major exception: Each class declaration (including struct and union declarations) must be terminated with a semicolon:
class Point { public: int x; int y; };
Now we can state the full syntax rule in C/C++:
- Terminate each statement with a semicolon.
- Don’t follow a closing brace with a semicolon unless it ends a class declaration, in which case it’s required.
Summary
Programming in C++ (or for that matter, in any language) is a life-long pursuit, and you never stop learning. In this article, I’ve looked at just the tip of the iceberg; however, in my programming experience going all the way back to the 1980s and even earlier, the issues in these article are points that, for me at least, come up again and again.
Among the more important ideas: Use the right operator for the right task (don’t confuse = and ==); pay attention to the effect of data types in math operations; be extra-careful about tempting shortcuts, such as while(n—); use local variables within for loops; and use meaningful symbolic names as much as possible. To be honest, I sometimes cut corners myself for very simple programs, but when you get into complex programming, following these guidelines will save you a lot of headacheif not heartache!