TDDD33 Programming (C++)
Style Guide
This page contain general C++ style rules we will use. You should use it to format your source code. In addition you should refer to the book and format assignment. To be consistent in the use of style is most important of all. Do not mix styles.
Includes
Includes have a space after include and occur first in file.
#include <iostream> #include "my_class.h" using namespace std;
Expressions
Expressions are written with spaces around operators. Sometimes it is more clear to write partial expressiong tight.
velocity = distance / time; f = a*x*x + b*x + c;
Parentheses is used whenever precedence is unclear.
return ((a || b) && (c || d));
Long lines should be suitably linebreaked or split in two expressions.
cout << "value of a = " << a
<< "value of b = " << b
<< "value of c = " << c
<< endl;
return ((adrian_is_home || bertram_is_at_work) &&
(ceasar_is_empereor || david_beat_goliat));
bool home_or_work_test = (adrian_is_home || bertram_is_at_work);
bool power_test = (ceasar_is_empereor || david_beat_goliat);
return home_or_work_test && power_test;
Statements and blocks
Braces (block parenteses) are always used and written on their own line. Braces are not indented, only the code within. One space is used after the keyword.
int sum = 0;
for (int i = 0; i < stop; ++i)
{
sum = secret_value[i];
}
while (!last())
{
next();
}
int positive = -1;
do
{
cout << "enter a positive value ";
cin >> positive;
}
while (positive < 0);
int main()
{
string selection;
while ( cin >> selection )
{
if (selection == "one")
{
do_one();
}
else if (selection == "two")
{
do_two();
}
else if (selection == "quit")
{
char confirm;
cin >> confirm;
if (confirm == 'Y' || confirm == 'Y')
{
return 0;
}
}
else
{
do_others();
}
}
return 0;
}
Identifiers
Use descriptive names on functions and variables. Emacs autocomplete names when you press [M-/] repeatedly ([M-S-7]).
Constants
Constants are writtent in upper case only. Words are separated with underscore.
const int SIZE_OF_APPLE_ARRAY = 4711;
Local variables
Local variable are written in lower case only. Words are separated with underscore.
int speed_of_car = 90; // km/h
Global variables
Global variables are not allowed. Do not use. You may use global constants.
Parameters
Parameters are written in lower case only. Words are separated with underscore. Never use the same name as a variable.
int example(int parameter_one, int parameter_two);
Functions
Function names are written in lower case. Words are separated with underscore.
double radians_to_degrees(double radians)
{
return radians * 360.0 / (2 * PI);
}
Classes
Class names use upper case on first letter in each word. Words are separated with underscore.
class Infix_To_Postfix;
Member Functions
Member functions use upper case letters on all but first word and no underscore.
class Array_Of_Five
{
public:
Array_Of_Five();
~Array_Of_Five();
int addElementAt(int i);
int getElementAt(int i);
private:
int data[5];
}
Page responsible: Klas Arvidsson
Last updated: 2010-08-21
