]> git.decadent.org.uk Git - ion3-doc.git/blob - cstyle.tex
[svn-upgrade] Integrating new upstream version, ion3-doc (20080103)
[ion3-doc.git] / cstyle.tex
1 \section{C coding style}
2
3 If you want to submit patches to Ion, you \textbf{must} follow my coding 
4 style, even if you think it is the root of all evil. We don't want
5 the code to be an incomprehensible mess of styles and I have better
6 things to do than fix other people's style to match mine. The style
7 should be obvious by studying the source, but here's a list of some
8 things to take note of.
9
10 \subsection{Whitespace}
11
12 \begin{itemize}
13   \item Indentations of 4 with spaces.
14     
15   \item No extra spaces between operators, delimiters etc. except
16     \begin{itemize}
17       \item around logical and, or (\code{&&}, \code{||})
18       \item around the conditional \code{a ? b : c}
19       \item after commas and semicolons
20     \end{itemize}  
21     In my opinion this helps pointing out arithmetic or other
22     expressions within logical expressions or parameter lists.
23     
24   \item All kinds of labels are out-tended to the level of the higher
25     level block. For example:
26
27 \begin{verbatim}    
28 void foo()
29 {
30 again:
31     switch(asdf){
32     case 1:
33         ...
34         break;
35     default:
36         ...
37         break;
38     }
39 }
40 \end{verbatim}
41 \end{itemize}
42
43 \subsection{Braces}
44
45 \begin{itemize}
46 \item Opening brace is at the end of the line, except in function
47     bodies, where it is at the beginning of the line following
48     the definition.
49
50 \item Never put the body of a control statement on the same line
51     with the statement (e.g. \verb!if(foo){ bar() }!).
52
53 For example, the block
54 \begin{verbatim}    
55 void foo(int a, int b)
56 {
57     if(a==b && c+d==e){
58         ...
59     }
60 }
61 \end{verbatim}
62
63 has correct style while the block
64
65 \begin{verbatim}   
66 void foo(int a,int b) {
67     if (a == b && c + d == e) {
68         ...
69     }
70 }
71 \end{verbatim}
72
73 does not.
74
75   \item The \code{else} keyword follows immediately after the closing brace of
76     previous \code{if}, if any. (This might change so I don't care if you put
77     it on the next line.)
78     
79   \item I have used the convention that control statement bodies containing
80     a single statement do not need braces around the block if, in case of
81     the \code{if} all the blocks in  \code{if ...  else if ... else}
82     contain just one statement. If you want to, just use braces in every 
83     case.
84 \end{itemize}
85
86 \subsection{Names}
87
88 \begin{itemize}
89   \item Function and variable names only have lower case letters. Type
90     names are in mixed case while constants and macros (\code{#define}s)
91     are in upper case letters.
92 \end{itemize}
93
94 \subsection{Miscellaneous}
95
96 \begin{itemize}
97   \item In the definition of a pointer variable, the asterisk is attached
98     to the variable name: \code{char *s;}. (One could claim this an 
99     exception to the second rule.)
100     
101   \item You might optionally want to use Jed's foldings to group blocks
102     of related code in a file to keep it organized:
103
104 \begin{verbatim}    
105 /*{{{ Many related functions */
106         
107 void code()
108 {
109     ... 
110 }
111
112 ...
113
114 /*}}}*/
115 \end{verbatim}
116 \end{itemize}
117
118 I think that's mostly it. Study the source when in doubt.