Menu

[r50]: / trunk / doc / coding_style.txt  Maximize  Restore  History

Download this file

148 lines (119 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
JsonRpc-Cpp coding style
=========================
I) Code layout
--------------
The code has to follow ANSI coding standard layout. Use 2 space indentation instead
of tabs.
void foo(int barc, char** barv)
{
int test = 0;
bool run = true;
size_t i = 0;
if(test)
{
/* do something */
}
else
{
/* do other thing */
}
while(run)
{
/* do loop */
}
for(i = 0 ; i < 2 ; i++)
{
/* do other loop */
}
do
{
/* do another loop */
}while(run);
}
The C++ comments ('//') can be used as well as the C comments ('/* */').
/* my short comment */
// another short comment
/* a longer comment
* on multiple lines
* ...
*/
II) Code organization
---------------------
The code has to follow these recommendations:
- Always initialize your variables;
- Put "static" keyword if functions / global variables are not used outside a C++ file (not in header file);
- Try to avoid static variables in functions (try to make reentrant things);
- Prefer using size_t/ssize_t instead of int/unsigned int when possible;
- Use "const" keyword for pointers and references if they should not been modified in function/method;
- Use "const" keyword for methods if they do not modified class variables;
- Put a space before and after operator +/-* i.e. a + b - ((c * 5) / 3);
- Put an empty line at the end of each file.
Naming has to follow these recommendations:
- File name has to be in lower case, words are separated by "_" (core files) or "-" (test and application-level files);
- Class and method name respect the CamelCase convention (MyClass::MyMethod);
- All class member variables name must be prefixed with "m_" (m_var);
- Function name should be in lower case, words are separeted by a "_" character (my_function);
- Each global variable must be prefixed with "g_" (g_run).
Each file, function, structure, class and global variable MUST be documented using doxygen.
Here is a complete example:
/**
* \file my_file.h
* \brief Short description.
* \author Name
* \date 2008
*/
#ifndef MY_FILE_H
#define MY_FILE_H
/**
* \struct my_struct
* \brief Short description.
*/
struct my_struct
{
int a; /**< Description of a */
char b; /**< Description of b */
};
/**
* \var g_my_var
* \brief Description of this global variable.
*/
struct my_struct* g_my_var = NULL;
/**
* \class MyClass
* \brief Short description of MyClass.
*/
class MyClass
{
public:
/**
* \brief Constructor.
*/
MyClass();
/**
* \brief Destructor.
*/
~MyClass();
/**
* \brief If count reach maximum.
* \param count parameter description
* \return true if count reach maximum, false otherwise
*/
bool IsMax(size_t count);
private:
/**
* \brief Member description.
*/
size_t m_count;
};
/**
* \brief Doing something.
* \param a number to use
* \param b char to use
* \return 0 if success, -1 otherwise
*/
int foo(int a, char b);
#endif /* MY_FILE_H */
III) Standards
---------------
In order to compile and run on many operating systems the code has to be portable. Thus
it is strongly recommended to write code which respects C++98 and POSIX standards.