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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 2002-2-1 (1.71)
original version by: Nikos Drakos, CBLU, University of Leeds
* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>5 C coding style</TITLE>
<META NAME="description" CONTENT="5 C coding style">
<META NAME="keywords" CONTENT="ionnotes">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="LaTeX2HTML v2002-2-1">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
<LINK REL="STYLESHEET" HREF="ionnotes.css">
<LINK REL="next" HREF="node7.html">
<LINK REL="previous" HREF="node5.html">
<LINK REL="up" HREF="ionnotes.html">
<LINK REL="next" HREF="node7.html">
</HEAD>
<BODY >
<DIV CLASS="navigation"><!--Navigation Panel-->
<A NAME="tex2html148"
HREF="node7.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html142"
HREF="ionnotes.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html136"
HREF="node5.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html144"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<A NAME="tex2html146"
HREF="node8.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html149"
HREF="node7.html">A. GNU Free Documentation</A>
<B> Up:</B> <A NAME="tex2html143"
HREF="ionnotes.html">Ion: Notes for the</A>
<B> Previous:</B> <A NAME="tex2html137"
HREF="node5.html">4 Miscellaneous design notes</A>
<B> <A NAME="tex2html145"
HREF="node1.html">Contents</A></B>
<B> <A NAME="tex2html147"
HREF="node8.html">Index</A></B>
<BR>
<BR></DIV>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL CLASS="ChildLinks">
<LI><A NAME="tex2html150"
HREF="node6.html#SECTION00061000000000000000"><SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">1</SPAN> Whitespace</A>
<LI><A NAME="tex2html151"
HREF="node6.html#SECTION00062000000000000000"><SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">2</SPAN> Braces</A>
<LI><A NAME="tex2html152"
HREF="node6.html#SECTION00063000000000000000"><SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">3</SPAN> Names</A>
<LI><A NAME="tex2html153"
HREF="node6.html#SECTION00064000000000000000"><SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">4</SPAN> Miscellaneous</A>
</UL>
<!--End of Table of Child-Links-->
<HR>
<H1><A NAME="SECTION00060000000000000000">
<SPAN CLASS="arabic">5</SPAN> C coding style</A>
</H1>
<P>
If you want to submit patches to Ion, you <SPAN CLASS="textbf">must</SPAN> follow my coding
style, even if you think it is the root of all evil. We don't want
the code to be an incomprehensible mess of styles and I have better
things to do than fix other people's style to match mine. The style
should be obvious by studying the source, but here's a list of some
things to take note of.
<P>
<H2><A NAME="SECTION00061000000000000000">
<SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">1</SPAN> Whitespace</A>
</H2>
<P>
<UL>
<LI>Indentations of 4 with spaces.
<P>
</LI>
<LI>No extra spaces between operators, delimiters etc. except
<UL>
<LI>around logical and, or (<TT>&&</TT>, <TT>||</TT>)
</LI>
<LI>around the conditional <TT>a ? b : c</TT>
</LI>
<LI>after commas and semicolons
</LI>
</UL>
In my opinion this helps pointing out arithmetic or other
expressions within logical expressions or parameter lists.
<P>
</LI>
<LI>All kinds of labels are out-tended to the level of the higher
level block. For example:
<P>
<PRE>
void foo()
{
again:
switch(asdf){
case 1:
...
break;
default:
...
break;
}
}
</PRE>
</LI>
</UL>
<P>
<H2><A NAME="SECTION00062000000000000000">
<SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">2</SPAN> Braces</A>
</H2>
<P>
<UL>
<LI>Opening brace is at the end of the line, except in function
bodies, where it is at the beginning of the line following
the definition.
<P>
</LI>
<LI>Never put the body of a control statement on the same line
with the statement (e.g. <code>if(foo){ bar() }</code>).
<P>
For example, the block
<PRE>
void foo(int a, int b)
{
if(a==b && c+d==e){
...
}
}
</PRE>
<P>
has correct style while the block
<P>
<PRE>
void foo(int a,int b) {
if (a == b && c + d == e) {
...
}
}
</PRE>
<P>
does not.
<P>
</LI>
<LI>The <TT>else</TT> keyword follows immediately after the closing brace of
previous <TT>if</TT>, if any. (This might change so I don't care if you put
it on the next line.)
<P>
</LI>
<LI>I have used the convention that control statement bodies containing
a single statement do not need braces around the block if, in case of
the <TT>if</TT> all the blocks in <TT>if ... else if ... else</TT>
contain just one statement. If you want to, just use braces in every
case.
</LI>
</UL>
<P>
<H2><A NAME="SECTION00063000000000000000">
<SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">3</SPAN> Names</A>
</H2>
<P>
<UL>
<LI>Function and variable names only have lower case letters. Type
names are in mixed case while constants and macros (<TT>#define</TT>s)
are in upper case letters.
</LI>
</UL>
<P>
<H2><A NAME="SECTION00064000000000000000">
<SPAN CLASS="arabic">5</SPAN>.<SPAN CLASS="arabic">4</SPAN> Miscellaneous</A>
</H2>
<P>
<UL>
<LI>In the definition of a pointer variable, the asterisk is attached
to the variable name: <TT>char *s;</TT>. (One could claim this an
exception to the second rule.)
<P>
</LI>
<LI>You might optionally want to use Jed's foldings to group blocks
of related code in a file to keep it organized:
<P>
<PRE>
/*{{{ Many related functions */
void code()
{
...
}
...
/*}}}*/
</PRE>
</LI>
</UL>
<P>
I think that's mostly it. Study the source when in doubt.
<P>
<P>
<DIV CLASS="navigation"><HR>
<!--Navigation Panel-->
<A NAME="tex2html148"
HREF="node7.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A>
<A NAME="tex2html142"
HREF="ionnotes.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A>
<A NAME="tex2html136"
HREF="node5.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A>
<A NAME="tex2html144"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A>
<A NAME="tex2html146"
HREF="node8.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A>
<BR>
<B> Next:</B> <A NAME="tex2html149"
HREF="node7.html">A. GNU Free Documentation</A>
<B> Up:</B> <A NAME="tex2html143"
HREF="ionnotes.html">Ion: Notes for the</A>
<B> Previous:</B> <A NAME="tex2html137"
HREF="node5.html">4 Miscellaneous design notes</A>
<B> <A NAME="tex2html145"
HREF="node1.html">Contents</A></B>
<B> <A NAME="tex2html147"
HREF="node8.html">Index</A></B> </DIV>
<!--End of Navigation Panel-->
</BODY>
</HTML>
|