SOME C++ CODE CONVENTIONS
Naming Conventions
| Convention | Example |
| Classes start with a capital. All internal words must start with
a capital too. |
class Circle ....
class MyVeryNiceCircle ... |
| Functions start with a lower-case letter. All internal words must
start with a capital. |
void draw ...
void drawCircle ... |
| Variables start with a lower case letter. All internal words must
be split with an underscore, and all have a lower case letter. |
int diameter;
int circle_diameter; |
A simple version of Hungarian Notation is used. The type is not specified,
but the scope/usage is:
| g
| Global variable
|
| m
| Class member attribute
|
| (no prefix)
| Local variable
|
| v
| Variable passed by value
|
| r
| Variable passed by reference
|
| p
| Pointer
|
|
int g_main_counter;
int m_diameter;
void foo(float v_i)
void foo(Circle& r_circle);
Circle* p_circle;
int* gp_main_counter;
|
Spacing/Indentation
| Convention | Example |
| Put the brackets on extra lines.
| void draw()
{
...
}
class Circle
{
...
}
|
| A space should follow after every ",", but not after "(", or before every ")". |
void multiplyNumbers(int n1, int n2)
{
....
}
int some_integer, some_other_integer; |
Comments (for documentation)
|
Doxygen is the documentation generating tool of choice. Therefore comments on
classes/functions must preceed the class/function and be in JavaDoc style
"/** Comment here */". All Doxygen comment must be in the header file.
|
/**
* Class for containing circle data and drawing it.
* This class is a lovely class that contains data about circles (center,
* radius). It also provides drawing functionality.
* \author Donald Knuth
* \see Ellipse
*/
class Circle
{
public:
/** Draw the circle on the screen. Only possible if initialized. */
void draw();
/**
* Set radius and center of the circle.
* \param radius New radius of the circle
* \param center New center of the circle
*/
void setCircle(int radius, struct Point center);
private:
int m_radius //!< The radius of the circle
struct Point m_center; //!< The center of the circle
};
|
| There are very many wonderful things possible with doxygen (the "\author" and
"\see" tags are only small examples). See
Doxygen homepage for more
info. Or you can directly have a look at how to document your code at
Doxygen:
Documenting the code. |
|