Flylib.com

Books Software

 
 
 

3.3 Naming Style

I l @ ve RuBoard

3.3 Naming Style

Names can contain both uppercase and lowercase letters . In this book we use all lowercase names for variables (e.g., source_ptr , current_index ). All uppercase is reserved for constants (e.g., MAX_ITEMS , SCREEN_WIDTH ). This convention is the classic convention followed by most C and C++ programs.

Many newer programs use mixed-case names (e.g., RecordsInFile ). Sometimes they use the capitalization of the first letter to indicate information about the variable. For example, recordsInFile might be used to denote a local variable while RecordsInFile would denote a global variable. (See Chapter 9 for information about local and global variables.) You should be careful when making up rules for your variables because the more complex the rules, the more likely someone will violate them or get confused .

One additional note on variable names: please use whole words. The problem with abbreviations is that there are too many different ways of abbreviating the same word. This is especially true when programmers think that to abbreviate a word, you write down the word and then cross out random letters. I've seen "Ground Point" named gp , ground_pt , gnd_pt , g_pnt , and many others. On the other hand, there's only one full spelling: ground_point .

Also, I work for a company which has people from 62 different countries working together to produce code. The non-English speakers have real difficulty looking up the more unusual abbreviations. Words in the dictionary make things much easier to understand for people who have to deal with the twin complexities of English and C++.

Which naming convention you use is up to you. It is more a matter of religion than of style. However, using a consistent naming style is extremely important. In this book we have chosen the first style ”lowercase variable names and uppercase constants ”and we use it throughout the book. (Note that this convention is growing less common as old-style #define constants are being replaced with new-style const ones.)

I l @ ve RuBoard
I l @ ve RuBoard

3.4 Coding Religion

Computer scientists have devised many programming styles. These include structured programming, top-down programming, and goto -less programming. Each of these styles has its own following or cult. I use the term "religion" because people are taught to follow the rules without knowing the reasons behind them. For example, followers of the goto -less cult will never use a goto statement, even when it is natural to do so.

The rules presented in this book are the result of years of programming experience. I have discovered that by following these rules, I can create better programs. You do not have to follow them blindly. If you find a better system, by all means use it. (If it really works, drop me a line. I'd like to use it, too.)

I l @ ve RuBoard
I l @ ve RuBoard

3.5 Indentation and Code Format

To make programs easier to understand, most programmers indent their programs. The general rule for a C++ program is to indent one level for each new block or conditional. In Example 3-1 there are three levels of logic, each with its own indentation level. The while statement is outermost. The statements inside the while are at the next level. The statement inside the if ( break ) is at the innermost level.

There are two styles of indentation, and a vast religious war is being waged in the programming community as to which is better. The first is the short form:

while (! done) { 
    std::cout << "Processing\n"; 
    next_entry(  ); 
} 

if (total <= 0) { 
    std::cout << "You owe nothing\n"; 
    total = 0; 
} else { 
    std::cout << "You owe " << total << " dollars\n"; 
    all_totals = all_totals + total; 
}

In this case, most of the curly braces are put on the same line as the statements. The other style puts the curly braces on lines by themselves :

while (! done) { 
    std::cout << "Processing\n"; 
    next_entry(  ); 
} 

if (total <= 0) { 
    std::cout << "You owe nothing\n"; 
    total = 0; 
} else { 
    std::cout << "You owe " << total << " dollars\n"; 
    all_totals = all_totals + total; 
}

Both formats are commonly used. You should use the format you feel most comfortable with. This book uses the short form. (It saves paper.)

The amount of indentation is left to the programmer. Two, four, and eight spaces are common. Studies have shown that a four-space indent makes the most readable code. You can choose any indent size as long as you are consistent.

Automatic Indenting

The vim editor is a vi -like program with many additional features. This includes the ability to automatically indent C++ programs. To turn on automatic four-space indentation, execute the commands:

:set cindent
:set sw=4
I l @ ve RuBoard