| 1 |
$Id: CODING_STYLE,v 1.7 2004/02/07 23:11:42 mbn Exp $ |
| 2 |
|
| 3 |
ClanLib coding style and conventions: |
| 4 |
-------------------------------------- |
| 5 |
|
| 6 |
1. All classes have a "CL_" prefix. Eg. CL_Display, CL_SoundBuffer... |
| 7 |
2. All macros have a "cl_" prefix. Eg. cl_assert, cl_info... |
| 8 |
|
| 9 |
3. We do NOT use K&R style C style. We use the special ClanSoft variant of |
| 10 |
the Microsoft style!! Please do NOT use K&R style and not GNU style either. |
| 11 |
Braces should look like this: |
| 12 |
|
| 13 |
int CL_MyClass::my_func(int arg1, int arg2) |
| 14 |
{ |
| 15 |
if (...) |
| 16 |
{ |
| 17 |
switch (whatever) |
| 18 |
{ |
| 19 |
case 1: |
| 20 |
break; |
| 21 |
|
| 22 |
case 2: |
| 23 |
{ |
| 24 |
int i = 5; |
| 25 |
... |
| 26 |
} |
| 27 |
break; |
| 28 |
} |
| 29 |
} |
| 30 |
else |
| 31 |
{ |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
Please try to follow this indenting style as closely as you can. If |
| 36 |
you are using Emacs you can set the coding style with: |
| 37 |
|
| 38 |
(c-set-style "linux") or C-c . linux |
| 39 |
|
| 40 |
4. We always use TABS, and NEVER SPACES to do indenting. This is because we |
| 41 |
want people to be able to pick their own tab size. I run with 4, but others |
| 42 |
like 8 and some like 2. |
| 43 |
|
| 44 |
So please don't use spaces. It will have a very bad effect when someone else |
| 45 |
uses another tab size than you. For instance, imagine the following was |
| 46 |
written by someone using tabs, and then you add a section with spaces: |
| 47 |
(all is written here with spaces so people will notice the difference seen |
| 48 |
with other tab sizes than their own) |
| 49 |
|
| 50 |
void my_func() |
| 51 |
{ |
| 52 |
int a,b,c,d; |
| 53 |
a = b + c/d; |
| 54 |
|
| 55 |
// added section with spaces: |
| 56 |
d = a; |
| 57 |
c = b/d; |
| 58 |
// end of space section |
| 59 |
|
| 60 |
do_something(a,b,c,d); |
| 61 |
} |
| 62 |
|
| 63 |
So it looks nice to you - but then we watch it with someone that has tab |
| 64 |
size 8: |
| 65 |
|
| 66 |
void my_func(); |
| 67 |
{ |
| 68 |
int a,b,c,d; |
| 69 |
a = b + c/d; |
| 70 |
|
| 71 |
// added section with spaces: |
| 72 |
d = a; |
| 73 |
c = b/d; |
| 74 |
// end of space section |
| 75 |
|
| 76 |
do_something(a,b,c,d); |
| 77 |
} |
| 78 |
|
| 79 |
Not very nice, is it? |
| 80 |
|
| 81 |
Many unix editors use a "smart" indenting algorithm which will fuck things |
| 82 |
even more up. They exchange spaces with tabs when reaching a given size |
| 83 |
(normally 8 or 4), but that just isn't very smart. The result is that some |
| 84 |
sections are spaces, and others are tabbed - all messed into one pile of |
| 85 |
junk. |
| 86 |
|
| 87 |
So please verify that your editor does indenting correct. This is |
| 88 |
important. |
| 89 |
|
| 90 |
5. Function names and variables are always in small, and underscore is used |
| 91 |
where other people often use a captial letter. |
| 92 |
Eg. MyVariable -> my_variable. |
| 93 |
|
| 94 |
6. Variable access functions have a set/get prefix. |
| 95 |
Eg. int size() -> int get_size() |
| 96 |
void size(int s) -> void set_size(int s) |
| 97 |
|
| 98 |
7. STL and variable names. |
| 99 |
|
| 100 |
Do NOT use the "using namespace std;" command. Not in API header or in source |
| 101 |
files. You may be the world champion in STL, but for us other mortals its NICE |
| 102 |
to be able to read what is STL and whats not. |
| 103 |
|
| 104 |
Also don't use two letter variable names and avoid to do aggressive shortening |
| 105 |
of common words (ie. cnt, num, refcnt, glph, fnt, idx). Its annoying to read |
| 106 |
if you have to stop up and think for every variable you encounter just to try |
| 107 |
figure out what it stands for. :) Yes we don't all speak english natively. |
| 108 |
|
| 109 |
8. The API documentation |
| 110 |
|
| 111 |
ClanLib uses a reference documentation system called pce2. It builds the |
| 112 |
reference by parsing the API header files where it looks for the |
| 113 |
documentation. There are the following types of documentation: |
| 114 |
|
| 115 |
1) Short description. |
| 116 |
|
| 117 |
The marker for short description is //: |
| 118 |
|
| 119 |
The short description is used at top of the class reference pages and |
| 120 |
function pages. Its supposed to be "a one sentence description". Example: |
| 121 |
|
| 122 |
//: Sprite image class. |
| 123 |
class CL_Sprite |
| 124 |
{ ... |
| 125 |
|
| 126 |
Short descriptions do not support markup and should not include any further |
| 127 |
markup tags (bold, italic, paragraph, etc etc). |
| 128 |
|
| 129 |
2) Long description. |
| 130 |
|
| 131 |
The marker for long description is //- |
| 132 |
|
| 133 |
The long description is also known as the "Detailed description:" part of |
| 134 |
the reference pages. This is where the class/functions purpose and |
| 135 |
functional details are explained. |
| 136 |
|
| 137 |
Long descriptions are supposed to put everything into html markup tags. Use |
| 138 |
the paragraph (<p>Text</p>) tag for text. Example: |
| 139 |
|
| 140 |
//: Sprite image class. |
| 141 |
//- <p>CL_Sprite is the image class of ClanLib. A sprite is a serie |
| 142 |
//- of images (each called a frame) that somehow have a connection |
| 143 |
//- to each others. It could be all the images needed to animate a |
| 144 |
//- man, or it could be all the different tiles of a tile map.</p> |
| 145 |
class CL_Sprite |
| 146 |
{ ... |
| 147 |
|
| 148 |
3) Function groups. |
| 149 |
|
| 150 |
Functions in a class is grouped into different sections. The typical ones |
| 151 |
are Construction, Attributes, Operations and Implementation. These groups |
| 152 |
are marked up like this: |
| 153 |
|
| 154 |
//! Construction: |
| 155 |
//! Attributes: |
| 156 |
//! Operations: |
| 157 |
//! Implementation: |
| 158 |
|
| 159 |
Any functions following such a group markup will belong to that group. The |
| 160 |
group markups are placed at the beginning of the line: |
| 161 |
|
| 162 |
//! Construction: |
| 163 |
public: |
| 164 |
//: Constructs a sprite object. |
| 165 |
//- <p>Blah blah, this will make a sprite, surprise!</p> |
| 166 |
CL_Sprite(); |
| 167 |
|
| 168 |
4) Function parameters. |
| 169 |
|
| 170 |
The marker for a function parameter is //param name: description |
| 171 |
|
| 172 |
Parameters of a function are desired to have a parameter description. An example: |
| 173 |
|
| 174 |
//! Operations: |
| 175 |
public: |
| 176 |
//: Draws the sprite onto back buffer. |
| 177 |
//- <p>The draw function will draw the current frame of the |
| 178 |
//- sprite at the specified position, using the alignment |
| 179 |
//- and other attributes specified for the sprite.</p> |
| 180 |
//param x: x position of where to draw the sprite. |
| 181 |
//param y: y position of where to draw the sprite. |
| 182 |
//param gc: Graphic context used as target. If null, current |
| 183 |
//param gc: selected display window will be used. |
| 184 |
void draw(int x, int y, CL_GraphicContext *gc = 0); |
| 185 |
|
| 186 |
5) See also. |
| 187 |
|
| 188 |
The marker for adding a "See also" reference is //also: text |
| 189 |
|
| 190 |
Example: |
| 191 |
|
| 192 |
//: Sprite image class. |
| 193 |
//- <p>CL_Sprite is the image class of ClanLib. A sprite is a serie |
| 194 |
//- of images (each called a frame) that somehow have a connection |
| 195 |
//- to each others. It could be all the images needed to animate a |
| 196 |
//- man, or it could be all the different tiles of a tile map.</p> |
| 197 |
//also: <a href="../../overview/sprite.html">Sprite overview</a> |
| 198 |
//also: CL_SpriteDescription |
| 199 |
class CL_Sprite |
| 200 |
{ ... |
| 201 |
|
| 202 |
6) Polymorph functions. |
| 203 |
|
| 204 |
If there exist several functions with the same name, then they share one |
| 205 |
common reference page for all of them. The documentation of them should be |
| 206 |
placed in front of the first function: |
| 207 |
|
| 208 |
//! Operations: |
| 209 |
public: |
| 210 |
//: Draws the sprite onto back buffer. |
| 211 |
//- <p>The draw function will draw the current frame of the |
| 212 |
//- sprite at the specified position, using the alignment |
| 213 |
//- and other attributes specified for the sprite.</p> |
| 214 |
//param x: x position of where to draw the sprite. |
| 215 |
//param y: y position of where to draw the sprite. |
| 216 |
//param dest: Destination rectangle of where to draw sprite. |
| 217 |
//param gc: Graphic context used as target. If null, current |
| 218 |
//param gc: selected display window will be used. |
| 219 |
void draw(int x, int y, CL_GraphicContext *gc = 0); |
| 220 |
|
| 221 |
void draw(CL_Rect &dest, CL_GraphicContext *gc = 0); |
| 222 |
|
| 223 |
7) Class groups and header file info. |
| 224 |
|
| 225 |
Each header file should contain below the GPL notice: |
| 226 |
//! clan{group}="{section}" |
| 227 |
{group} denotes the ClanLib group (eg Core, Display, GUI) |
| 228 |
{section} denotes the group section (eg "Controls" for GUI) |
| 229 |
|
| 230 |
To denote which base header file a class belongs to: |
| 231 |
//! header={fname}.h |
| 232 |
{fname} denotes the base header file name eg "display" |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
There is more things than there - but I think this summarizes the most |
| 237 |
important issues. In general, just do like the other source files do. |