]> git.decadent.org.uk Git - ion3.git/blob - doc/ionnotes/node6.html
[svn-upgrade] Integrating new upstream version, ion3 (20070506)
[ion3.git] / doc / ionnotes / node6.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2
3 <!--Converted with LaTeX2HTML 2002-2-1 (1.71)
4 original version by:  Nikos Drakos, CBLU, University of Leeds
5 * revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
6 * with significant contributions from:
7   Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
8 <HTML>
9 <HEAD>
10 <TITLE>5 C coding style</TITLE>
11 <META NAME="description" CONTENT="5 C coding style">
12 <META NAME="keywords" CONTENT="ionnotes">
13 <META NAME="resource-type" CONTENT="document">
14 <META NAME="distribution" CONTENT="global">
15
16 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
17 <META NAME="Generator" CONTENT="LaTeX2HTML v2002-2-1">
18 <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
19
20 <LINK REL="STYLESHEET" HREF="ionnotes.css">
21
22 <LINK REL="next" HREF="node7.html">
23 <LINK REL="previous" HREF="node5.html">
24 <LINK REL="up" HREF="ionnotes.html">
25 <LINK REL="next" HREF="node7.html">
26 </HEAD>
27
28 <BODY >
29
30 <DIV CLASS="navigation"><!--Navigation Panel-->
31 <A NAME="tex2html149"
32   HREF="node7.html">
33 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
34 <A NAME="tex2html143"
35   HREF="ionnotes.html">
36 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
37 <A NAME="tex2html137"
38   HREF="node5.html">
39 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
40 <A NAME="tex2html145"
41   HREF="node1.html">
42 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> 
43 <A NAME="tex2html147"
44   HREF="node8.html">
45 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> 
46 <BR>
47 <B> Next:</B> <A NAME="tex2html150"
48   HREF="node7.html">A. The GNU General</A>
49 <B> Up:</B> <A NAME="tex2html144"
50   HREF="ionnotes.html">Ion: Notes for the</A>
51 <B> Previous:</B> <A NAME="tex2html138"
52   HREF="node5.html">4 Miscellaneous design notes</A>
53  &nbsp; <B>  <A NAME="tex2html146"
54   HREF="node1.html">Contents</A></B> 
55  &nbsp; <B>  <A NAME="tex2html148"
56   HREF="node8.html">Index</A></B> 
57 <BR>
58 <BR></DIV>
59 <!--End of Navigation Panel-->
60 <!--Table of Child-Links-->
61 <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
62
63 <UL CLASS="ChildLinks">
64 <LI><A NAME="tex2html151"
65   HREF="node6.html#SECTION00061000000000000000"><SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">1</SPAN> Whitespace</A>
66 <LI><A NAME="tex2html152"
67   HREF="node6.html#SECTION00062000000000000000"><SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">2</SPAN> Braces</A>
68 <LI><A NAME="tex2html153"
69   HREF="node6.html#SECTION00063000000000000000"><SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">3</SPAN> Names</A>
70 <LI><A NAME="tex2html154"
71   HREF="node6.html#SECTION00064000000000000000"><SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">4</SPAN> Miscellaneous</A>
72 </UL>
73 <!--End of Table of Child-Links-->
74 <HR>
75
76 <H1><A NAME="SECTION00060000000000000000">
77 <SPAN CLASS="arabic">5</SPAN> C coding style</A>
78 </H1>
79
80 <P>
81 If you want to submit patches to Ion, you <SPAN  CLASS="textbf">must</SPAN> follow my coding 
82 style, even if you think it is the root of all evil. We don't want
83 the code to be an incomprehensible mess of styles and I have better
84 things to do than fix other people's style to match mine. The style
85 should be obvious by studying the source, but here's a list of some
86 things to take note of.
87
88 <P>
89
90 <H2><A NAME="SECTION00061000000000000000">
91 <SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">1</SPAN> Whitespace</A>
92 </H2>
93
94 <P>
95
96 <UL>
97 <LI>Indentations of 4 with spaces.
98
99 <P>
100 </LI>
101 <LI>No extra spaces between operators, delimiters etc. except
102     
103 <UL>
104 <LI>around logical and, or (<TT>&amp;&amp;</TT>, <TT>||</TT>)
105 </LI>
106 <LI>around the conditional <TT>a ? b : c</TT>
107 </LI>
108 <LI>after commas and semicolons
109     
110 </LI>
111 </UL>  
112     In my opinion this helps pointing out arithmetic or other
113     expressions within logical expressions or parameter lists.
114
115 <P>
116 </LI>
117 <LI>All kinds of labels are out-tended to the level of the higher
118     level block. For example:
119
120 <P>
121 <PRE>    
122 void foo()
123 {
124 again:
125     switch(asdf){
126     case 1:
127         ...
128         break;
129     default:
130         ...
131         break;
132     }
133 }
134 </PRE>
135 </LI>
136 </UL>
137
138 <P>
139
140 <H2><A NAME="SECTION00062000000000000000">
141 <SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">2</SPAN> Braces</A>
142 </H2>
143
144 <P>
145
146 <UL>
147 <LI>Opening brace is at the end of the line, except in function
148     bodies, where it is at the beginning of the line following
149     the definition.
150
151 <P>
152 </LI>
153 <LI>Never put the body of a control statement on the same line
154     with the statement (e.g. <code>if(foo){ bar() }</code>).
155
156 <P>
157 For example, the block
158 <PRE>    
159 void foo(int a, int b)
160 {
161     if(a==b &amp;&amp; c+d==e){
162         ...
163     }
164 }
165 </PRE>
166
167 <P>
168 has correct style while the block
169
170 <P>
171 <PRE>   
172 void foo(int a,int b) {
173     if (a == b &amp;&amp; c + d == e) {
174         ...
175     }
176 }
177 </PRE>
178
179 <P>
180 does not.
181
182 <P>
183 </LI>
184 <LI>The <TT>else</TT> keyword follows immediately after the closing brace of
185     previous <TT>if</TT>, if any. (This might change so I don't care if you put
186     it on the next line.)
187
188 <P>
189 </LI>
190 <LI>I have used the convention that control statement bodies containing
191     a single statement do not need braces around the block if, in case of
192     the <TT>if</TT> all the blocks in  <TT>if ...  else if ... else</TT>
193     contain just one statement. If you want to, just use braces in every 
194     case.
195 </LI>
196 </UL>
197
198 <P>
199
200 <H2><A NAME="SECTION00063000000000000000">
201 <SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">3</SPAN> Names</A>
202 </H2>
203
204 <P>
205
206 <UL>
207 <LI>Function and variable names only have lower case letters. Type
208     names are in mixed case while constants and macros (<TT>#define</TT>s)
209     are in upper case letters.
210 </LI>
211 </UL>
212
213 <P>
214
215 <H2><A NAME="SECTION00064000000000000000">
216 <SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">4</SPAN> Miscellaneous</A>
217 </H2>
218
219 <P>
220
221 <UL>
222 <LI>In the definition of a pointer variable, the asterisk is attached
223     to the variable name: <TT>char *s;</TT>. (One could claim this an 
224     exception to the second rule.)
225
226 <P>
227 </LI>
228 <LI>You might optionally want to use Jed's foldings to group blocks
229     of related code in a file to keep it organized:
230
231 <P>
232 <PRE>    
233 /*{{{ Many related functions */
234         
235 void code()
236 {
237     ... 
238 }
239
240 ...
241
242 /*}}}*/
243 </PRE>
244 </LI>
245 </UL>
246
247 <P>
248 I think that's mostly it. Study the source when in doubt.
249
250 <P>
251
252 <P>
253
254 <DIV CLASS="navigation"><HR>
255 <!--Navigation Panel-->
256 <A NAME="tex2html149"
257   HREF="node7.html">
258 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
259 <A NAME="tex2html143"
260   HREF="ionnotes.html">
261 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
262 <A NAME="tex2html137"
263   HREF="node5.html">
264 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
265 <A NAME="tex2html145"
266   HREF="node1.html">
267 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> 
268 <A NAME="tex2html147"
269   HREF="node8.html">
270 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> 
271 <BR>
272 <B> Next:</B> <A NAME="tex2html150"
273   HREF="node7.html">A. The GNU General</A>
274 <B> Up:</B> <A NAME="tex2html144"
275   HREF="ionnotes.html">Ion: Notes for the</A>
276 <B> Previous:</B> <A NAME="tex2html138"
277   HREF="node5.html">4 Miscellaneous design notes</A>
278  &nbsp; <B>  <A NAME="tex2html146"
279   HREF="node1.html">Contents</A></B> 
280  &nbsp; <B>  <A NAME="tex2html148"
281   HREF="node8.html">Index</A></B> </DIV>
282 <!--End of Navigation Panel-->
283
284 </BODY>
285 </HTML>