Standard convention for programming with C++ can be found in several other languages. Not only is the commands similar to other languages, but so is the structure format.
Now C++ doesn’t care about white space (tabs, spaces, new line characters, etc), but it makes it easier to read and debug, and work with others. While a majority of your schooling is done individually, a majority of your work will be done in groups, and thus you will want your code to me legible within a group, otherwise you will be ridiculed for it.
Comments
Properly commenting your code is extremely important. A lot of C/C++ commands are abbreviations or symbols which means reading and inferring meaning can be difficult.
The C language gave us one type of comment, the block comment. It starts with /* , and ends with */. It can cover multiple lines, one full line, or part of a line.
/* This is a comment */
/*
I am also a comment
*/
for(int i = 0; i < 10; i++ ) { /* a for loop to count to 10, but this is a comment */
}
This type of comment is often used before a function to define the inputs, what is returned, and what the function will be doing.
If this is used for a large multi-line comment, many developers will put a “*” on each new line to help it visually stand out. Most IDEs will color code it, but you cannot rely on this, which is why developers will use this trick.
/*
* This is a nice
* long, multi-line
* comment.
*/
Notice how the stars all line up for visual effect.
C++ added a single line comment. Everything after the comment starter is ignored. To start the comment, you will use two forward slashes. If it goes to be “multi-line” you will need to start each line with the double forward slash.
// I'm a comment.
// I'm the start of a multi-line comment
-- but I'm an error, since no slashes preceded my line.
// I'm the start of a C++ style multi-line comment
// And I can finish it properly because I have the multi-line slashes.
Spacing
While the compiler doesn’t care about spacing, spacing makes it easier for us to read. Therefore, we try to put a space between each command, variable, etc. Consider the following and determine which is easier to read and thus maintain.
int x;
x = 8+7-z*5; // condensed "correct" but hard to read.
x = 8 + 7 - z * 5; // easier to read.
// Won't effect the size of your compiled code.
Indenting
Python’s rules for indenting were part of the programming language. In C++, those rules don’t exist, but are generally followed for ease of reading.
Indent for each programming block, or body of code. That includes if / else statements, loop bodies, function bodies, etc.
Bodies, or blocks, of code in C++ are generally surrounded by curly braces: { and }. They should go on their own separate line, and be aligned with the parent, not the body of the statement.
Errors
Errors in C/C++ are like any other language. You have syntax errors, run time errors and logic errors.
Since C/C++ programs are compiled before running, you cannot run a program until all syntax errors are resolved. This is different from Python which was an interactive, interrupted language which would let you run part way, until you found an syntax error.
We still need to test our programs however, as the compiler cannot tell us if we run into a run-time or logic error, as it cannot determine those.
Basic Programming Style and Notes for C++ was originally found on Access 2 Learn