Currently under-construction.
INTRODUCTION
What is a set of standards and why am I reading this?
The purpose of C++ standards is to define a C++ coding standard that should be adhered to when writing code. ISO 9000 and the Capability Maturity Model (CMM) state that coding standards are mandatory for any organization with quality goals. Standards provide
indications aimed at helping programmers to meet the following requirements on a program:
1.be free of common types of errors
2.be maintainable by different programmers
3.be easy to read and understand
4.have a consistent style
There are no hard and fast rules when it comes to programming style only the restrictions enforced by the compiler, however it is important to be
consistent. Below we are going to touch on some of the styles and guidelines that should be followed as much as possible but without trying to enforce any concrete rules.
Most large companies have a set of standards that are enforced. If you are working for such a company you are being paid to write code subject based on what they want you to write, and in a style that must match up to their standards. The main reason being maintainability along with the above mentioned points.
People should also note that no standard should be followed to the letter, in fact most standards will have clauses that allow you to disregard certain points or have a priority listing(should, would, will). While you may be thinking that some of the standards you have seen seem completely silly, they do not always need to be followed, some can be disregarded and you can even create your own rules if the current project calls for such workarounds.
This will be at most a style guide, used to develop your own style but while still conforming to the needs of the community. It will cover 3 main topics: Naming, Style and Coding.
For a very in depth collection of styles and techniques
Bjarne Stroustrup's C++ Style and Technique FAQ is a very good read:
http://www2.research.att.com/~bs/bs_faq2.html
NAMING
Names are the heart of programming. In the past people believed knowing
someone’s true name gave them magical power over that person. If you can
think up the true name for something, you give yourself and the people coming
after power over the code. Don’t laugh!
1. Naming of files.
The names of header and implementation files are generally the same with different suffix file type, they are also commonly named the same as the class. Some operating systems are non-case sensitive while others are case sensitive so for cross compatibility reasons you should stick to one style of case naming, most programmers stick to lowercase file-names to avoid confusion.
other examples?
2. Naming of Classes, Functions, Variables, Constants, and Methods.
Meaningful names are important, recall the above statement about power over a name.
Usually functions and methods perform some type of action so naming the function in a way that describes what the function is doing is generally the best approach e.g.
WriteDataToFile( )
rather than
DataFile( )
Classes are often nouns. By making function names verbs and following other naming conventions programs can be read more naturally. Make every variable name descriptive, limit the use of abbreviations or letter-words. It’s worth writing words completely since it makes the code much more readable. Beware however that when trying to find a good name, you don’t end up with with something like ’the_variable_for_the_loop’, use a proper English word for it like counter’ or ’iterator’, but with a prefix attatched to be more descriptive like 'appleCounter'. English is a rich language and trying to find a correctly fitting word is important for code readability, cleanness and variation. Whenever in doubt, just use a thesaurus.
Suffixes are sometimes useful:
• Max - to mean the maximum value something can have.
• Cnt - the current count of a running count variable.
• Key - key value.
For example: RetryMax to mean the maximum number of retries, RetryCnt to mean the current retry count.
• Prefixes are sometimes useful:
• Is - to ask a question about something. Whenever someone sees Is they will know it’s a question.
• Get - get a value.
• Set - set a value.
For example: GetMaxOrangeCnt( )
Some standard variables are used for often recurring tasks. Below is a list of those that are accepted (for those that follow the first, are used as a backup within the same scope) :
i , j , k : integer counter (used in for loops)
x, y, z : multiplication, or graph points.
r , c , d , t : row, column, depth, time (used for array/pointer cells)
it : STL-like iterator
c : char (temporary)
<type>_it : STL-like iterator of a certain type for differentiation amongst types
tmp_<type> : eg. tmp_qstring, tmp_int, tmp_float for variables that are solely used for the storage of temporary intermediate values
Notice the above I have listed both 'c' for column and 'c' for char, using both of these in the same scope would be a compilation error.
C++ is a case sensitive language so sometimes it may be tempting to use the same name for a variable with only a change in a case. This is generally considered poor practise as it becomes difficult to read and debug. e.g.
1 2 3
|
unsigned int truck;
unsigned double trucK;
const char Truck='T';
|
However another acceptable way of naming global members is with a 'g' or global constants with a 'gc' as a prefix, also the underscore is commonly used as a global identifier. e.g.
1 2 3 4 5 6 7 8
|
// header
private:
int _balance; // member that stores balance of ones account
// or
int gBalance;
// or
int g_balance;
};
|
Additionally Hungarian notation can be used and combined with descriptive English names. For more information see this link:
http://en.wikipedia.org/wiki/Hungarian_notation
For output of units of information, b for bits, B for bytes, and o for octets. SI prefixes are mostly used capitalized (K instead of k). If the prefix is a power of two, an i is added to it (KiB instead of KB). Otherwise, the prefix is a power of ten. The user should be aware of this difference where appropriate.