Programming style is a personal thing, and a difficult one to learn. I have seen a programmer produce completely unreadable code, bad enough to make other people swear at their screens, while sincerely attempting to generate an exceptionally clear and simple result.
Here are some hints for writing better C++ programs, in no particular order:
Tab
key. In both vi
and
emacs
, 8-character tab stops are the default, but
many Windows-based editors violate the standard by using
4-character tabs.
If your editor produces files with 4-character tab stops, you can clean up the file with the following commands:
% expand -4 < foo.cc | unexpand -a > foo_clean.cc % mv foo_clean.cc foo.cc
At the same time, don't go too crazy. Excessive spacing (e.g., many blank lines inside a function) hides structure just as effectively as no spacing at all.
i
(too short and not descriptive),
pointer_to_left_side_of_the_maze
(too long),
harvey
(not descriptive), and
pointerToInteger
(either redundant or misleading,
depending on the declaration).
NumberOfProfessors
,
numberOfStudents
, and
number_of_staff
).
while (i >= 0) { ... }and Allman style:
while (i >= 0) { ... }Professor Kuenning prefers a third style (you're welcome to ask why, outside of class):
while (i >= 0) { ... }Yet a fourth style is available in the C++ mode of emacs:
while (i >= 0) { ... }We don't care which of these you use, so long as you stick with it. Don't invent a fifth style unless you're prepared to argue why it's outstandingly good.
if(i==0) j++;looks very similar to an assignment statement or complex function call (a problem that is aggravated by the lack of whitespace). It's much better to put the consequent on a separate line so that the indentation makes it clear that a test is happening:
if (i == 0) j++;Remember that whitespace is cheap!
int values[100];
, use:
const int MAX_VALUES = 100; ... int values[MAX_VALUES];This has two benefits: it makes your code much clearer (when the reader sees a loop limit of 100, he or she doesn't have to wonder whether it's a loop to MAX_VALUES or to the number of centimeters in a meter), and it makes it much easier to change the constant later (if you need to double the size of the
values
array, you only have to change one
line, instead of searching the entire program for every
occurrence of "100" -- and forgetting the place where you used
99 instead of 100-1).
goto
statement at all costs. If you
can't figure out how to get along without a goto
,
ask your professor or one of the graders. A
goto
is never unavoidable,
although there is one situation (which should not arise in
your assignments) where it serves as a multilevel
break
and is therefore acceptable.
// Initialize array to zero before setting nonzero values for (int i = 0; i < ARRAY_SIZE; i++) values[i] = 0;rather than this:
for (int i = 0; // i is loop index, start at zero i < ARRAY_SIZE; // ARRAY_SIZE is how far loop goes i++) // Increment i values[i] = 0; // Set ith value to zero
==
operator to do anything other than compare for equality, even
if a different function might be useful in your code.
Remember that overloading is intended to enhance readability,
not to make your code easier to type.
Exception: There are some operator overloads
that are so common that they are allowable even though you
might find them a bit odd. Examples include the string
concatenation operator +
, and the iterator
operators ++
and --
(for stepping),
*
and []
(for access to data), and
bool
casts (for testing).
These are general guidelines only; more on coding style will be covered in class. As discussed in class, a readable program is more important than a working one, if you have to make a choice.
© 2002, Geoff Kuenning
This page is maintained by Geoff Kuenning.