updates.
[silc.git] / lib / doc / programming_conv.html
1 <big><b>Programming Conventions</b></big>
2
3 <br />&nbsp;<br />
4 The SILC Toolkit has been programmed with a specific programming style that
5 is consistent across all libraries and interfaces.  The programming style
6 defines for example naming conventions for functions, structures, macros,
7 enumerations, and other constants.
8
9
10 <br />&nbsp;<br />&nbsp;<br />
11 <b>Naming Conventions</b>
12
13 <br />&nbsp;<br />
14 <b>Macros and Defines</b>
15
16 <br />&nbsp;<br />
17 Macros are always capitalised and include underscores to separate words
18 in the name.  All macros start with the "SILC_" prefix.  Example:
19
20 <br />&nbsp;<br />
21 <tt>
22 #define SILC_PACKET_PADLEN(__packetlen, __blocklen)      \<br />
23 &nbsp;&nbsp;SILC_PACKET_DEFAULT_PADLEN - (__packetlen) % \<br />
24 &nbsp;&nbsp;&nbsp;&nbsp;((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN)
25 </tt>
26
27 <br />&nbsp;<br />
28 Also defines (#define) are always capitalised and include underscores to
29 separate words in the name.  Also all defines start with the "SILC_" prefix.
30
31 <br />&nbsp;<br />
32 <b>Structures</b>
33
34 <br />&nbsp;<br />
35 All structure names begin with "Silc" prefix, and the name is mixed-case,
36 for example: SilcClientConnection, SilcCommandPayload.  Many of the 
37 structures used in SILC are actually private structures, and application
38 cannot access them directly.  In these cases the structures are forward
39 declared in the public header, and the implementation of the structure
40 is in the source file.  In these case application does not need to know
41 the contents of the structure, and is usually provided with a helper API
42 to access the structure when needed.
43
44 <br />&nbsp;<br />
45 In the most of the cases the forward declaration for a structure is pointer, 
46 for example:
47
48 <br />&nbsp;<br />
49 <tt>typedef struct SilcClientStruct *SilcClient;</tt>
50
51 <br />&nbsp;<br />
52 Application should always use the type defined pointer instead of the
53 actual structure.
54
55 <br />&nbsp;<br />
56 <b>Functions</b>
57
58 <br />&nbsp;<br />
59 Function naming uses the common naming convention used in Toolkit.  All
60 functions are always lowercase and they use underscores.  The name of
61 the function always starts with prefix "silc_".  The name tells what
62 the function do.  The name of a function is constructed from following parts:
63
64 <br />&nbsp;<br />
65 <tt>silc_(module)_(function)</tt>
66
67 <br />&nbsp;<br />
68 The (module) is the library, or interface this functions is part of.  For
69 example: "cipher", "config", "command", "packet", etc.
70
71 <br />&nbsp;<br />
72 The (function) is the description of the functionality of the function.
73 For example: "read", "new_id", "register", "find_by_name", etc.  Examples:
74
75 <br />&nbsp;<br />
76 <tt>
77 silc_server_packet_send<br />
78 silc_server_packet_send_to_channel<br />
79 silc_idcache_del_by_id<br />
80 silc_schedule_init<br />
81 silc_protocol_excute_final<br />
82 silc_buffer_alloc
83 </tt>
84
85 <br />&nbsp;<br />
86 When function registers something the name of the function generally is
87 "silc_function_register" and unregistering is done with
88 "silc_function_unregister".  When function allocates something it
89 is "silc_function_alloc" and when freeing it is
90 "silc_function_free".  Respectively, with init/uninit functions.
91
92 <br />&nbsp;<br />
93 <b>Enumerations</b>
94
95 <br />&nbsp;<br />
96 Enumerations are always capitalised and include underscores to separate
97 words in the name.  All enumerations start with the "SILC_" prefix.  Also,
98 usually all enumerations are type defined to a specific name which can
99 be used as type for the enumeration.  Example:
100
101 <br />&nbsp;<br />
102 <tt>
103 typedef enum {<br />
104 &nbsp;&nbsp;SILC_EXAMPLE_ENUM_NONE,<br />
105 &nbsp;&nbsp;SILC_EXAMPLE_ENUM_LIST,<br />
106 &nbsp;&nbsp;SILC_EXAMPLE_ENUM_STATUS,<br />
107 } SilcExampleEnum;
108 </tt>
109
110 <br />&nbsp;<br />
111 The naming for the type definition for the enumerations follow the
112 normal naming convention; the name starts with "Silc" prefix and the
113 name is mixed-case.
114
115
116 <br />&nbsp;<br />&nbsp;<br />
117 <b>Layout</b>
118
119 <br />&nbsp;<br />
120 <b>Indentation</b>
121
122 <br />&nbsp;<br />
123 The indendation in the source code is 2 characters, and tabulators are
124 not used.  Example piece of code:
125
126 <br />&nbsp;<br />
127 <tt>
128 void silc_client_free(SilcClient client)<br />
129 {<br />
130 &nbsp;&nbsp;if (client) {<br />
131 &nbsp;&nbsp;&nbsp;&nbsp;if (client->rng)<br />
132 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;silc_rng_free(client->rng);<br />
133 &nbsp;&nbsp;&nbsp;&nbsp;silc_free(client);<br />
134 &nbsp;&nbsp;}<br />
135 }
136 </tt>
137
138 <br />&nbsp;<br />
139 <b>Placing Braces</b>
140
141 <br />&nbsp;<br />
142 Generally the braces placing the SILC code follows the K&R style; the
143 opening of the brace is put to the last on the line, and the closing brace
144 is on first on its own line, except for functions.  Examples:
145
146 <br />&nbsp;<br />
147 <tt>
148 if (condition) {<br />
149 &nbsp;&nbsp;silc_something();<br />
150 &nbsp;&nbsp;silc_something_more();<br />
151 }
152 </tt>
153
154 <br />&nbsp;<br />
155 <tt>
156 int silc_client_function()<br />
157 {<br />
158 &nbsp;&nbsp;return 0;<br />
159 }
160 </tt>
161
162 <br />&nbsp;<br />
163 <tt>
164 if (condition) {<br />
165 &nbsp;&nbsp;something;<br />
166 &nbsp;&nbsp;silc_something_more();<br />
167 } else {<br />
168 &nbsp;&nbsp;something_else;<br />
169 }
170 </tt>
171
172 <br />&nbsp;<br />
173 <tt>
174 if (condition) {<br />
175 &nbsp;&nbsp;something;<br />
176 &nbsp;&nbsp;silc_something_more();<br />
177 } else if (other_condition) {<br />
178 &nbsp;&nbsp;something;<br />
179 &nbsp;&nbsp;silc_something_more();<br />
180 } else {<br />
181 &nbsp;&nbsp;something_else;<br />
182 }
183 </tt>
184
185 <br />&nbsp;<br />
186 <b>Header Files</b>
187 <br />&nbsp;<br />
188
189 Standard anti-nesting method is used in the header files to avoid 
190 multiple inclusion of the header file.  Example:
191
192 <br />&nbsp;<br />
193 <tt>
194 #ifndef SILCHEADER_H<br />
195 #define SILCHEADER_H<br />
196 ...<br />
197 #endif /* SILCHEADER_H */
198 </tt>
199
200 <br />&nbsp;<br />
201 All public header files have the "silc" prefix in the filename, for example:
202 silcclient.h, silcprivate.h, silcutil.h.  There are other header files in
203 the Toolkit as well.  Application should not directly include these headers,
204 however if needed it may access them.
205
206 <br />&nbsp;<br />
207 Every header file also includes a copyright notice.