1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
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 -->
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">
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">
20 <LINK REL="STYLESHEET" HREF="ionnotes.css">
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">
30 <DIV CLASS="navigation"><!--Navigation Panel-->
33 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
36 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
39 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
42 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
45 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A>
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 <B> <A NAME="tex2html146"
54 HREF="node1.html">Contents</A></B>
55 <B> <A NAME="tex2html148"
56 HREF="node8.html">Index</A></B>
59 <!--End of Navigation Panel-->
60 <!--Table of Child-Links-->
61 <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
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>
73 <!--End of Table of Child-Links-->
76 <H1><A NAME="SECTION00060000000000000000">
77 <SPAN CLASS="arabic">5</SPAN> C coding style</A>
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.
90 <H2><A NAME="SECTION00061000000000000000">
91 <SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">1</SPAN> Whitespace</A>
97 <LI>Indentations of 4 with spaces.
101 <LI>No extra spaces between operators, delimiters etc. except
104 <LI>around logical and, or (<TT>&&</TT>, <TT>||</TT>)
106 <LI>around the conditional <TT>a ? b : c</TT>
108 <LI>after commas and semicolons
112 In my opinion this helps pointing out arithmetic or other
113 expressions within logical expressions or parameter lists.
117 <LI>All kinds of labels are out-tended to the level of the higher
118 level block. For example:
140 <H2><A NAME="SECTION00062000000000000000">
141 <SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">2</SPAN> Braces</A>
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
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>).
157 For example, the block
159 void foo(int a, int b)
161 if(a==b && c+d==e){
168 has correct style while the block
172 void foo(int a,int b) {
173 if (a == b && c + d == e) {
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.)
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
200 <H2><A NAME="SECTION00063000000000000000">
201 <SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">3</SPAN> Names</A>
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.
215 <H2><A NAME="SECTION00064000000000000000">
216 <SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">4</SPAN> Miscellaneous</A>
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.)
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:
233 /*{{{ Many related functions */
248 I think that's mostly it. Study the source when in doubt.
254 <DIV CLASS="navigation"><HR>
255 <!--Navigation Panel-->
256 <A NAME="tex2html149"
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"
264 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
265 <A NAME="tex2html145"
267 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
268 <A NAME="tex2html147"
270 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A>
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 <B> <A NAME="tex2html146"
279 HREF="node1.html">Contents</A></B>
280 <B> <A NAME="tex2html148"
281 HREF="node8.html">Index</A></B> </DIV>
282 <!--End of Navigation Panel-->