Conventions for code

Good C + + program is not only based on advanced programming techniques but also a good programming style that 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 then 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 to gain the course’s higher grades (A, B or 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 naturally clear from the syntax.
4 Only one statement per line.
5 Add blank line (not more than ONE) above keywords like while, if, for, switch… etc, between functions and between associated set of sections, to make the code more readable.
6 Indent the code correctly according to editor preset standard. The default format can be changed under the menu Tools->Options->Text Editor. Preferably use the built-in “automatic” formatting tool available in Visual Studio under the menu Edit->Advanced->Format Document alt. Format Selection.
7 Curly braces around code blocks { … and … }, placed either in a straight line under 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 shall have meaningful, explanatory name. Ex: sum
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. Ex: sqrt(x)
14 Parenthese following after switch, for, while, if … etc. are written with (one) space. 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 consequent with 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