TDDE18 Programming (C++)
Style Guide
This page contain general C++ style rules you should use in the course to format your source code nicely. In addition you should refer to the book and format assignment. When programming it is most important that you style is consistent and clear. Do not mix styles.
Includes
Includes have a space after include and occur first in file. Standard includes use less and greater than symbols, own includes use double quoutes. Standard includes are listed first. Empty lines separate similar groups of statements.
Good example | Bad example |
---|---|
#include <iostream> #include <iomanip> #include "my_class.h" using namespace std; |
#include"iostream" #include<my_class.h> #include<iomanip> using namespace std; |
Expressions
Expressions are written with spaces around operators. Sometimes it is more clear to write partial expressiong tight.
Good example | Bad example |
---|---|
velocity = distance / time; f = a*x*x + b*x + c; |
velocity=distance/time; f=a * x*x+b *x+c; |
Parentheses is used whenever precedence is unclear.
Good example | Bad example |
---|---|
return ((a || b) && (c || d)); |
return a||b&&c||d; |
Long lines should be suitably linebreaked or split in two expressions.
Good example |
---|
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; |
Bad example |
---|
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)); return adrian_is_home||bertram_is_at_work&&ceasar_is_empereor||david_beat_goliat; |
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. Empty lines separate coherent code.
Good example | Bad example |
---|---|
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; } |
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.
Good example | Bad example |
---|---|
const int SIZE_OF_APPLE_ARRAY = 4711; |
const int aplesiz=4711; |
Local variables
Local variable are written in lower case only. Words are separated with underscore.
Good example | Bad example |
---|---|
int speed_of_car = 90; // km/h |
int spEEdYCar=90; |
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 in the function.
Good example | Bad example |
---|---|
int example(int parameter_one, int parameter_two); |
int example(int P, int Q); |
Functions
Function names are written in lower case. Words are separated with underscore.
Good example | Bad example |
---|---|
double radians_to_degrees(double radians) { return radians * 360.0 / (2 * PI); } |
double r2d2(double R) { return R*180/PI; } |
Classes
Class names use upper case on first letter in each word. Words are separated with underscore.
Good example | Bad example |
---|---|
class Infix_To_Postfix; |
class infixpostfix; |
Member Functions
Member functions use upper case letters on all but first word and no underscore.
Good example | Bad example |
---|---|
class Array_Of_Five { public: Array_Of_Five(); ~Array_Of_Five(); int addElementAt(int i); int getElementAt(int i); private: int data[5]; } |
class Array_Of_Five { public: Array_Of_Five(); ~Array_Of_Five(); int Add_element_at(int i); int Get_element_at(int i); private: int data[5]; } |
Page responsible: Sam Le
Last updated: 2017-08-06