Debian Codegraph @

doc/style-guide.txt

raw
1 I've redone this file to hopefully make it a little easier to read through
2 and quickly get some idea what to do. There are 3 sections -
3 section 1 is current programming style/hints for developers to make
4 things easier. Section 2 is programming guide for new addition.
5 Section 3 is notes for making patches.
6
7 ------------------------------------------------------------------------------
8 Section 1 - currently used conventions/hints for new code writers:
9
10 1) variable abbreviations - op is short for object pointer, ob is for
11 object, and pl is for player.
12
13 2) Some functions are named using the conventions above - the naming reflects
14 what options they take (insert_ob_in_ob takes 2 object structures)
15
16 3) Indentation is 4 spaces.
17
18 4) Some structure elements should never be accessed directly - rather,
19 there are other functions to use the values.
20
21 object->owner: This contains the owner id for this object. Use
22 set_owner and get_owner instead. Directly using object->owner
23 is likely to get unpredictable results.
24
25 object->nrof: This contains the number of an object.
26 Since changing this will change the weight of an object, direct
27 access should also be avoided. Use decrease_ob_nr, split_ob,
28 and insert_ob_in_... - the later will merge the objects if
29 applicable.
30
31 5) If using insert_ob_in_map and plan to do further actions with the object,
32 check and make sure the object still exists after insertion - it is possible
33 that the object gets destroyed while being inserted.
34
35 ------------------------------------------------------------------------------
36 Section 2 - Style guide for new additions:
37
38 1) Use descriptive variable names. op and pl should only be used for
39 temporary variables (cycling through the list or the like). For variables
40 well defined, use an accurate name (ie, hitter, sack, etc).
41
42 2) Only add name options with #ifdef's to the config file if the behaviour
43 seriously changes the game. Adding a new spell does not warrant an
44 #ifdef. There are already too many options in the config.h file.
45
46 3) Log errors/diagnostics with the LOG function. When doing so,
47 please include the function name - this is especially true for errors.
48
49 4) If you want to add special debug code for certain compiles, generate
50 a unique #define for it - don't use the global DEBUG. For example,
51 NEWCS_DEBUG.
52
53 5) Try to use C99 types whenever possible. Use the one
54 of appropriate size/type. If not sure, go for the next size up. Do
55 not ever write code assuming that any of those will have an exact number
56 of bits - those types only mean that you will get at least that many
57 bits - you may get more.
58
59 6) The exception to #5 above is strings. Continue to use 'char', since
60 the signedness of functions that take string options can differ system
61 to system, and generate excessive warnings if the wrong sign is used.
62
63 7) When adding new function, include a comment of what the function is
64 supposed to do, what options it takes, and what if any value it returns.
65 This makes debugging of such functions easier, and also makes it better
66 known to other developers if that function might be useful to them.
67
68 8) Try to keep lines to less than 80 columns when possible. This is not
69 a strict requirement - don't break up some complex comparison because the
70 line would otherwise be 83 characters long. Xterms can be resized to most
71 any width. However, use your judgement on whether breaking up a long
72 line would make something more or less readable.
73
74 9) Assume all names use one namespace. For example, if there is a
75 struct called spell, don't make the name of an optional parameter spell.
76 This will break on C compilers that follow the spec strictly
77
78 10) As a followup on 9 above, don't use nonstandard gcc extensions
79 (ability to nest functions, declare arrays with variable bounds, etc.)
80 Likewise, don't use special system functions - don't
81 assume the target system will be bsd or svr4 - if using a potentially non
82 standard function, add checks in the autoconf script and include a version
83 of the function in case it is not on that system. They key word here is
84 portability - don't assume everyone else has the same system as you do.
85
86 11) Write code that can easily be maintained in the future, not code that
87 is easiest to write at that second. This basically means don't do the
88 quick and ugly hack, but instead fix it properly.
89
90 12) Use 4 space indentation. While a lot of old code may have 2 space,
91 the move to 4 space will make future readability easier.
92
93 Take from http://www.jwz.org/doc/tabs-vs-spaces.html
94
95 In Emacs, to set the mod-N indentation used when you hit the TAB key, do this:
96
97 (setq c-basic-indent 2)
98 or (setq c-basic-indent 4)
99
100 To cause the TAB file-character to be interpreted as mod-N indentation, do this:
101
102 (setq tab-width 4)
103 or (setq tab-width 8)
104
105 To cause TAB characters to not be used in the file for compression, and for only
106 spaces to be used, do this:
107
108 (setq indent-tabs-mode nil)
109
110 To keep myself honest (that is, to ensure that no tabs ever end up in source
111 files that I am editing) I also do this in my .emacs file:
112
113 (defun java-mode-untabify ()
114 (save-excursion
115 (goto-char (point-min))
116 (while (re-search-forward "[ \t]+$" nil t)
117 (delete-region (match-beginning 0) (match-end 0)))
118 (goto-char (point-min))
119 (if (search-forward "\t" nil t)
120 (untabify (1- (point)) (point-max))))
121 nil)
122
123 (add-hook 'java-mode-hook
124 '(lambda ()
125 (make-local-variable 'write-contents-hooks)
126 (add-hook 'write-contents-hooks 'java-mode-untabify)))
127
128 That ensures that, even if I happened to insert a literal tab in the file by
129 hand (or if someone else did when editing this file earlier), those tabs get
130 expanded to spaces when I save. This assumes that you never use tabs in places
131 where they are actually significant, like in string or character constants, but
132 I never do that: when it matters that it is a tab, I always use '\t' instead.
133
134 To get vim to interpret tab as an ``indent'' command instead of an insert-a-tab
135 command, do this:
136
137 set softtabstop=2
138
139 To set the mod-N indentation used when you hit the tab key in vim (what Emacs
140 calls c-basic-indent), do this:
141
142 set shiftwidth=2
143
144 To cause the TAB file-character to be displayed as mod-N in vi and vim (what
145 Emacs calls tab-width), do this:
146
147 set tabstop=4
148
149 To cause TAB characters to not be used in the file for compression, and for only
150 spaces to be used (what emacs calls indent-tabs-mode), do this:
151
152 set expandtab
153
154 12.1) I work on several projects and each uses a different indent format. What
155 do I do?
156
157 Taken from an email from Eric Estabrooks <[email protected]>, which is
158 based on info found in the linux kernel.
159
160 add to your .emacs
161 ; linux kernel c mode
162 (defun linux-c-mode ()
163 "C mode with adjusted defaults for use with the Linux kernel."
164 (interactive)
165 (c-mode)
166 (c-set-style "K&R")
167 (setq c-basic-offset 8))
168
169 ; set linux kernel mode for anything in /usr/src/linux*
170 (setq auto-mode-alist (cons '("/usr/src/linux.*/.*\\.[ch]$" . linux-c-mode)
171 auto-mode-alist))
172
173 13)
174 /*
175 * do block
176 * comment like
177 * this
178 */
179
180 /*
181 and not
182 like this
183 */
184
185 /* if you are doing a single line comment, this method is fine */
186
187 Its much easier to spot the block comments if they all start with *,
188 and these comments tend to be worth noticing.
189
190 14) As discussed on irc, the preferred style for expressions is like this:
191
192 if (expression) {
193 statement;
194 statement;
195 }
196
197 if <space> (expression), the space between the if and expression is required.
198
199 NOT like this:
200
201 if (expression)
202 {
203 statement;
204 statement;
205 }
206
207 15) The preferred style of formal parameters:
208
209 void myFooFunction(param1, param2, param3) {
210 statement;
211 statement;
212 }
213
214 No space after the left paren, no space before the right paren.
215 Comma right after the formal param, space right after the comma.
216
217
218 16) Local variable names. Just a rules of thumb.
219
220 These are ok:
221
222 int mylongvarname;
223 int my_long_var_name;
224
225 Please do NOT use caps expect for typedefs, enums and defines.
226
227 ------------------------------------------------------------------------------
228 Section 3 - sending in patches:
229
230 1) Please send patches on a bug fix or feature enhancement basis
231 individually, and not make mega patches. A diff that changes 10
232 things is first more difficult for me to look over and understand as
233 unrelated changes might be going on. It is also harder for me to reject
234 part of a patch (feature X is nice, but Y doesn't work).
235
236 2) Please state in the message included with the patch what it fixes/changes.
237 Too often, I get patches which is just a bunch of source code, and I have
238 no idea if I want to incorporate it, or even if the bug is still there.
239 Please also state what version of crossfire the diff is for.
240
241 3) I will assume any patches mailed directly to me are to be included.
242 If posting a patch on the mailing list (either source or ftp location),
243 please explicity state whether or not you want that patch incorporated
244 into the master source. Many times, a patch may be made available on
245 an expiremental basis which is not ready for widespread distribution.
246
247 4) When making patches, please make context diffs. Please also include
248 the directory that the file is in (run the diff in the top level
249 directory). Please make 5 line context diffs - large line context diffs
250 are fine if you think that may make it easier.
251
252 Example:
253
254 'diff -c5 (oldfile) (newfile)'
255
256 You can also do diffs of entire directories. Do do this, type:
257
258 'diff -c5 -r (old_directory) (new_directory)'
259
260 An example:
261
262 'diff -c5 -r crossfire-0.90.1 crossfire-0.90.2'
263
264 5) Gnu diff will include files that did not exist before. Other diff
265 programs may not do this.
266
267 6) If your diff looks excessively long and you made a lot of formatting
268 changes, you can add -w to the diff options to have it ignore whitespace.
269 Note that this will then mean that those formatting changes will then be lost.
270
271 7) There is no need to make a seperate diff file for each file
272 different (ie, treasure.diff, player.diff, etc). Assuming you follow steps
273 1-6, all the diffs can be contained in one file, and patch will deal with
274 it just fine.
275
276 8) If you need to send a map, new archetypes, or other new files where
277 a diff doesn't make since, a uuencoded tar file will work just fine.
278
279 Mail all patches to [email protected]