Menu

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

Download this file

135 lines (107 with data), 2.4 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
JsonRpc-Cpp coding style
=========================
I) Code layout
The software uses the ANSI coding standard layout. Use 2 space indentation instead
of tabs.
void foo(int barc, char** barv)
{
int test = 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 recommandations:
- Always initialize your variables;
- Put "static" keyword when functions / global variables is not used outside the file;
- Try to avoid static variables in functions (try to make reentrant things);
- All member variables of a class must be prefixed with "m_";
- Each global variable must be prefixed with "g_";
- Put an empty line at the end of each file.
Each file, function, structure, classe 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 */
};
/**
* \class MyClass
* \brief Short description of MyClass.
*/
class MyClass
{
public:
/**
* \brief Constructor.
*/
MyClass();
/**
* \brief Destructor.
*/
~MyClass();
/**
* \brief A function
* \param count parameter description
* \return true if success, false otherwise
*/
bool AFunction(unsigned int count);
private:
/**
* \brief Member description.
*/
unsigned int m_count;
};
/**
* \var g_my_var
* \brief Description of this global variable.
*/
struct my_struct* g_my_var = NULL;
/**
* \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 respect C++98 and POSIX standards.