| 1 |
The FreeTDS coding style for C code is close to K&R/Linux kernel style. |
| 2 |
This document makes explicit the coding style found through most of the |
| 3 |
project. There are likely to be portions of code that do not follow this |
| 4 |
style, but ideally all would. |
| 5 |
|
| 6 |
We are of course not trying to dictate how you code, but patches or submissions |
| 7 |
to git should be converted to this style prior to committing. The indent |
| 8 |
program found on most Unix systems will do this nicely. |
| 9 |
|
| 10 |
Code Blocks |
| 11 |
----------- |
| 12 |
|
| 13 |
Blocks of code should start with the { on the line with the control statement. |
| 14 |
|
| 15 |
For example, |
| 16 |
|
| 17 |
for (i=0;i<x;i++) { |
| 18 |
... |
| 19 |
} |
| 20 |
|
| 21 |
This applies to for, do, while, switch, if and any other looping or branching |
| 22 |
constructs. The exception to this is function declarations, in which the { |
| 23 |
should follow the declaration, as in |
| 24 |
|
| 25 |
void |
| 26 |
foo(int bar) |
| 27 |
{ |
| 28 |
... |
| 29 |
} |
| 30 |
|
| 31 |
Indenting |
| 32 |
--------- |
| 33 |
|
| 34 |
Indenting should be done with one tab character per level. Default tabspace |
| 35 |
is 8 characters. You can set in your editor whatever tabspace you want |
| 36 |
but be sure that you indent using tabs and that line break work well with a |
| 37 |
tabspace of 8. |
| 38 |
|
| 39 |
Variables |
| 40 |
--------- |
| 41 |
|
| 42 |
Variables in FreeTDS should be all lower case with words separated by '_' |
| 43 |
(underscore). Furthermore, brevity is valued but not at the expense of clarity. |
| 44 |
For instance, the variable 'minor_ver' is clear enough in context and doesn't |
| 45 |
need to be labeled 'minor_version'. The counter example to this is that the |
| 46 |
variable 'can' is rather ambiguous and be expanded to 'canceled' for clarity. |
| 47 |
|
| 48 |
The sole exception to this is arguments to the ODBC functions. ODBC because of |
| 49 |
its lineage uses hungarian notation for the names of the function arguments. |
| 50 |
Variables that are internal to the functions should still follow the coding |
| 51 |
style, but function arguments retain their Microsoft given names. |
| 52 |
|
| 53 |
Variable declarations at the top of the function should be left aligned. This is |
| 54 |
a departure from K&R style but one I find is helpful when scanning through the |
| 55 |
code. Example, |
| 56 |
|
| 57 |
void |
| 58 |
foo(int bar) |
| 59 |
{ |
| 60 |
int i; |
| 61 |
|
| 62 |
for (i=0;i<bar;i++) { |
| 63 |
|
| 64 |
... |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
a single space is generally preferred to separate the type from the variable |
| 69 |
name, although some code pushes variable names to the right for clarity. |
| 70 |
Which ever looks better is fine. |
| 71 |
|
| 72 |
Function Declarations |
| 73 |
--------------------- |
| 74 |
|
| 75 |
Function declarations started out a little like this: |
| 76 |
|
| 77 |
void |
| 78 |
foo(int bar, int baz) |
| 79 |
{ |
| 80 |
} |
| 81 |
|
| 82 |
with no spaces between the function name 'foo' and the left paren, between the |
| 83 |
left paren and the first argument, nor the last argument and the right paren. A |
| 84 |
single space should follow the comma after each argument. |
| 85 |
|
| 86 |
In recent times, the functions have been moving towards the GNU style |
| 87 |
declaration with the return type and modifiers on the line above, and the next |
| 88 |
line starting with the name of the function |
| 89 |
|
| 90 |
static void |
| 91 |
foo(int bar, int baz) |
| 92 |
{ |
| 93 |
} |
| 94 |
|
| 95 |
Spacing remains the same, but having the function aligned to the left side makes |
| 96 |
searching for functions, and not the calls to them, easier. This style is now |
| 97 |
preferred, but much of the code does not implement it. |
| 98 |
|
| 99 |
When creating a function with no arguments there should be no spacing between |
| 100 |
the parens. |
| 101 |
|
| 102 |
void |
| 103 |
foobar() |
| 104 |
{ |
| 105 |
} |
| 106 |
|
| 107 |
Comments |
| 108 |
-------- |
| 109 |
|
| 110 |
FreeTDS is a fairly portable package and runs on a number of platforms that |
| 111 |
have either no C++ style comments or need special switches to turn them on |
| 112 |
(the AIX C compiler comes to mind). Therefore, all comments must be C-style |
| 113 |
comments and not the C++ // comments. |
| 114 |
|
| 115 |
void |
| 116 |
foo() |
| 117 |
{ |
| 118 |
// incorrect |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
foo() |
| 123 |
{ |
| 124 |
/* correct */ |
| 125 |
} |
| 126 |
|
| 127 |
The second comments related item is function documentation. While not really |
| 128 |
widely implemented yet, we are moving towards doxygen (JavaDoc) comments so |
| 129 |
that function declarations can be extracted into a programmers manual. |
| 130 |
|
| 131 |
See http://www.stack.nl/~dimitri/doxygen/manual.html for details. |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
indent(1) |
| 136 |
--------- |
| 137 |
|
| 138 |
The indent tool is a handy way to spiff up your formatting and bring it into |
| 139 |
conformance. It will compile faster! No, it won't. But it will be more |
| 140 |
understandable to the lesser beings who want to try to follow you. |
| 141 |
|
| 142 |
We are currently using GNU indent 2.2.6. |
| 143 |
|
| 144 |
These are the settings we use in our .indent.pro profile file. I have listed our |
| 145 |
choice first, its alternative second (in case we change our minds). When the |
| 146 |
default follows some other setting, I've placed a '#' to indicate it's (sort of) |
| 147 |
commented out, and won't appear in .indent.pro. Same goes for options e.g. |
| 148 |
'-troff' that have no opposite. |
| 149 |
|
| 150 |
-bad, -nbad If -bad is specified, a blank line is forced after every |
| 151 |
block of declarations. Default: -nbad. |
| 152 |
-bap, -nbap If -bap is specified, a blank line is forced after every |
| 153 |
procedure body. Default: -nbap. |
| 154 |
-nbbb, -bbb If -bbb is specified, a blank line is forced before every |
| 155 |
block comment. Default: -nbbb. |
| 156 |
-nbc, -bc If -bc is specified, then a newline is forced after each |
| 157 |
comma in a declaration. -nbc turns off this option. The |
| 158 |
default is -bc. |
| 159 |
-br, -bl K & R braces, see above. |
| 160 |
-c33 The column in which comments on code start. The default. |
| 161 |
#cd -n Indent declarations as code. |
| 162 |
-ncdb, -cdb Allow single-line comments e.g.: |
| 163 |
/* this is a comment */ |
| 164 |
The default is -cdb. |
| 165 |
-ce, -nce Forcing `else's to cuddle up to the |
| 166 |
immediately preceding `}'. The default is -ce. |
| 167 |
#cin Sets the continuation indent to be n. -ci defaults to |
| 168 |
the same value as -i. |
| 169 |
-cli0 Causes case labels to be indented n tab stops to the |
| 170 |
right of the containing switch statement. -cli0 -.5 |
| 171 |
causes case labels to be indented half a tab stop. The |
| 172 |
default is -cli0. |
| 173 |
-d0 Controls the placement of comments which are not to the |
| 174 |
right of code. The default -d1 means that such comments |
| 175 |
are placed one indentation level to the left of code. |
| 176 |
Specifying -d0 lines up these comments with the code. |
| 177 |
-di1 Specifies the indentation, in character positions, from a |
| 178 |
declaration keyword to the following identifier. The de- |
| 179 |
fault is -di16. |
| 180 |
-dj, -ndj -dj left justifies declarations. -ndj indents declara- |
| 181 |
tions the same as code. The default is -ndj. |
| 182 |
-nei, -ei Disable special else-if processing. |
| 183 |
-nfc1, -fc1 Disable formatting comments that start in column 1. |
| 184 |
-i8 The number of spaces for one indentation level. |
| 185 |
-ts8 Tab stops at 8 postitions. |
| 186 |
-ut Use Tabs |
| 187 |
-ip, -nip Enable the indentation of parameter declarations |
| 188 |
from the left margin. The default is -ip. |
| 189 |
-l80 Maximum length of an output line. The default is 75. |
| 190 |
-lp, -nlp Line up code surrounded by parenthesis: |
| 191 |
p1 = first_procedure(second_procedure(p2, p3), |
| 192 |
third_procedure(p4,p5)); |
| 193 |
-lp is the default. |
| 194 |
#npro Causes the profile files, `./.indent.pro' and |
| 195 |
-pcs, -npcs Procedure calls will have a space inserted between the |
| 196 |
name and the `('. The default is -npcs. |
| 197 |
-psl, -npsl The return type of a procedure is placed on the line above |
| 198 |
its name. The default is -psl. |
| 199 |
-sc, -nsc Put asterisks (`*'s) at the left edge of all comments. |
| 200 |
-nsob, -sob If -sob is specified, indent will swallow optional blank |
| 201 |
declarations. Default: -nsob. |
| 202 |
#st Causes indent to take its input from stdin, and put its |
| 203 |
#Ttypename Adds typename to the list of type keywords. Names accu- |
| 204 |
mulate: -T can be specified more than once. You need to |
| 205 |
that are defined by typedef - nothing will be harmed if |
| 206 |
typedef causes a syntactic change in the language and in- |
| 207 |
#troff Causes indent to format the program for processing by |
| 208 |
-nv, -v -v turns on `verbose' mode; -nv turns it off. When in |
| 209 |
size statistics at completion. The default is -nv. |