Conventions for code

Good C++ programs are not only based on advanced programming techniques but also a good programming style, which follows established coding standard. This is essential to effectively debug, reuse, maintain and develop programs that you or others have written. An important goal of the course is hence to learn writing code that supports a good program quality.

The following are general requirements for your solutions to fullfill. Coding standard is included in quality assessment and in order to gain any of the course’s higher grades (A-C) the following should be well satisfied.

Common conventions for writing C++ programs in a good style
1  Always write a commentary header to the program with the file name, date, programmer and brief description of the program’s mission.
2  Always write a summary header comment to every user-defined function.
3  Always comment code where its intent is not obvious from the syntax.
4  Only one statement per line!
5  Add blank line (not more than ONE) above keywords like while, if, for … etc, between functions and between logically associated sets of statements, to make the code more readable (but otherwise no unnecessary spacing!).
6  Indent the code correctly according to editor preset standard. The default format can be changed in the menubar’s Tools->Options->Text Editor. Preferably use the built-in “automatic” formatting tool available in Visual Studio! See menubar’s Edit->Advanced->Format Document (alt. Format Selection).
7  Curly braces around code blocks { … and … } should be placed either in a straight line below initial keyword:
if (x > 0)
{
// statement(s);
}
… or like this:
if (x > 0) {
// statement(s);
}
 8  Indent code inside blocks, ie. between { … and … }, with tabs. Exceptionally, two or three whitespaces can be used, but never mix tabs and spaces.
 9  Variables and other identifiers, like function names, are written in lower case (also in beginning of name!) and must have meaningful, explanatory names. Ex: numberOfDigits ( but never m, n, x…!)
10  Composite names are written in upper case separation (“camel case”) or _. Use consistently one or the other type. Ex: circleArea or circle_area
11  Constants are written in UPPERCASE.
12  #define’s (macros) are written using only CAPS.
13  Parentheses to functions are written without spaces between name and parenthesis. Ex: sqrt(x)
14  Parentheses of the for, while, if, switch, … are written with spaces. Ex: if (x> 0)
15  Use spaces between operators and operands in an expression, to increase readability. Ex:
for (int i = 1; i < 111; i++) … instead of:     for (int i=1;i<111;i++)

Most important: Be ALWAYS consistent regarding your choice of notation or standard!

Welcome to CoursePress

en utav Linnéuniversitets lärplattformar. Som inloggad student kan du kommunicera, hålla koll på dina kurser och mycket mer. Du som är gäst kan nå de flesta kurser och dess innehåll utan att logga in.

Läs mer lärplattformar vid Linnéuniversitetet

Student account

To log in you need a student account at Linnaeus University.

Read more about collecting your account

Log in LNU