]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/CodingStyle.yo
release: 1.0.2
[lilypond.git] / Documentation / CodingStyle.yo
1 report(CodingStyle - standards while programming for GNU
2 LilyPond)(Han-Wen Nienhuys and Jan Nieuwenhuizen)()()
3
4 nsect(DESCRIPTION)
5
6 We use these standards while doing programming for GNU LilyPond.  If
7 you do some hacking, we appreciate it if you would follow this rules,
8 but if you don't, we still like you.
9
10 Functions and methods do not return errorcodes, but use assert for
11 checking status.
12
13 quote(
14
15 A program should be light and agile, its subroutines
16 connected like a string of pearls.  The spirit and intent of
17 the program should be retained throughout.  There should be
18 neither too little nor too much, neither needless loops nor
19 useless variables, neither lack of structure nor overwhelming
20 rigidity.
21
22 A program should follow the 'Law of Least
23 Astonishment'.  What is this law?  It is simply that the
24 program should always respond to the user in the way that
25 astonishes him least.
26
27 A program, no matter how complex, should act as a
28 single unit.  The program should be directed by the logic
29 within rather than by outward appearances.
30
31 If the program fails in these requirements, it will be
32 in a state of disorder and confusion.  The only way to correct
33 this is to rewrite the program.
34
35 -- Geoffrey James, "The Tao of Programming"
36 )
37
38
39 nsubsect(LANGUAGES)
40
41 C++, /bin/sh and python are preferred.  Perl is not.
42
43 nsubsect(FILES)
44
45 Definitions of classes that are only accessed via pointers
46 (*) or references (&) shall not be included as include files.
47
48 filenames
49
50 verb(
51         ".hh"   Include files
52         ".cc"   Implementation files
53         ".icc"  Inline definition files
54         ".tcc"  non inline Template defs
55 )
56
57 in emacs:
58
59 verb(
60         (setq auto-mode-alist
61               (append '(("\\.make$" . makefile-mode)
62                         ("\\.cc$" . c++-mode)
63                         ("\\.icc$" . c++-mode)
64                         ("\\.tcc$" . c++-mode)
65                         ("\\.hh$" . c++-mode)
66                         ("\\.pod$" . text-mode)         
67                         )
68                       auto-mode-alist))
69 )
70
71
72 The class Class_name_abbreviation is coded in file(class-name-abbr.*)
73
74
75 nsubsect(INDENTATION)
76
77 in emacs:
78
79 verb(
80         (add-hook 'c++-mode-hook
81                   '(lambda() (c-set-style "gnu")
82                      )
83                   )
84 )
85
86 If you like using font-lock, you can also add this to your file(.emacs):
87
88 verb(
89         (setq font-lock-maximum-decoration t)
90         (setq c++-font-lock-keywords-3 
91               (append
92                c++-font-lock-keywords-3
93                '(("\\b\\([a-zA-Z_]+_\\)\\b" 1 font-lock-variable-name-face)
94                ("\\b\\([A-Z]+[a-z_]+\\)\\b" 1 font-lock-type-face))
95                ))
96 )
97
98 nsubsect(CLASSES and TYPES:)
99
100 verb(
101         This_is_a_class
102         AClass_name     (for Abbreviation_class_name)
103 )
104
105 nsubsect(MEMBERS)
106
107 verb(
108         Class::member()
109         Type Class::member_type_
110         Type Class::member_type()
111 )
112
113 the code(type) is a Hungarian notation postfix for code(Type). See below
114
115 nsubsect(MACROS)
116
117 Macros should be written completely in uppercase
118
119 The code should not be compilable if proper macro declarations are not
120 included. 
121
122 Don't laugh. It took us a whole evening/night to figure out one of
123 these bugs, because we had a macro that looked like
124 code(DECLARE_VIRTUAL_FUNCTIONS()). 
125
126 nsubsect(BROKEN CODE)
127
128 Broken code (hardwired dependencies, hardwired constants, slow
129 algorithms and obvious limitations) should be marked as such: either
130 with a verbose TODO, or with a short "ugh" comment.
131
132 nsubsect(COMMENTS)
133
134 The source is commented in the DOC++ style.  Check out doc++ at
135 lurl(http://www.zib.de/Visual/software/doc++/index.html)
136
137 verb(
138         /*
139                 C style comments for multiline comments.
140                 They come before the thing to document.
141                 [...]
142         */
143
144
145         /**
146                 short description.
147                 Long class documentation.
148                 (Hungarian postfix)
149
150                 TODO Fix boring_member()
151         */
152         class Class {
153                 /**
154                   short description.
155                   long description
156                 */
157
158                 Data data_member_;
159
160
161                 /**
162                         short memo. long doco of member()
163                         @param description of arguments
164                         @return Rettype
165                 */
166                 Rettype member(Argtype);
167
168                 /// memo only
169                 boring_member() {
170                         data_member_ = 121; // ugh
171                 }
172         };
173 )
174
175         
176 Unfortunately most of the code isn't really documented that good.
177
178
179 nsubsect(MEMBERS (2))
180
181 Standard methods:
182
183 verb(
184         ///check that *this satisfies its invariants, abort if not.
185         void OK() const
186
187         /// print *this (and substructures) to debugging log
188         void print() const
189
190         /**
191         protected member. Usually invoked by non-virtual XXXX()
192         */
193         virtual do_XXXX()
194
195         /**add some data to *this.
196         Presence of these methods usually imply that it is not feasible to this
197         via  a constructor
198         */
199         add (..)
200
201         /// replace some data of *this
202         set (..)
203 )
204
205 nsubsect(Constructor)
206
207 Every class should have a default constructor.  
208
209 Don't use non-default constructors if this can be avoided:
210
211 verb(
212         Foo f(1)
213 )
214
215 is less readable than
216
217 verb(
218         Foo f;
219         f.x = 1
220 )
221
222 or 
223
224 verb(
225         Foo f(Foo_convert::int_to_foo (1))
226 )
227
228 nsect(HUNGARIAN NOTATION NAMING CONVENTION)
229
230 Proposed is a naming convention derived from the so-called
231 em(Hungarian Notation).
232
233 nsubsect(Hungarian)
234
235 The Hungarian Notation was conceived by or at least got its name from,
236 the hungarian programmer Charles Simonyi.  It is a naming convention 
237 with the aim to make code more readable (for fellow programmers), and 
238 more accessible for programmers that are new to a project.
239
240 The essence of the Hungarian Notation is that every identifier has a
241 part which identifies its type (for functions this is the result
242 type).  This is particularly useful in object oriented programming,
243 where a particular object implies a specific interface (a set of
244 member functions, perhaps some redefined operators), and for
245 accounting heap allocated memory pointers and links.
246
247 nsubsect(Advantages)
248
249 Another fun quote from Microsoft Secrets:
250
251 quote(
252         The Hungarian naming convention gives developers the ability
253         to read other people's code relatively easily, with a minmum
254         number of comments in the source code.  Jon De Vann estimated
255         that only about 1 percent of all lines in the Excel product
256         code consist of comments, but the code is still very
257         understandable due to the use of Hungarian: "if you look at
258         our source code, you also notice very few comments.  Hungarian
259         gives us the ability to go in and read code..."
260 )
261
262 Wow! If you use Hungarian you don't have to document your software!
263 Just think of the hours I have wasted documenting while this "silver bullet"
264 existed. I feel so stupid and ashamed!  (Didn't MMM-Brooks say `There is
265 no silver bullet?' --HWN)
266
267
268 nsubsect(Disadvantages)
269
270 itemize(
271 it()more keystrokes (disk space!)
272 it()it looks silly code(get_slu_p())
273 it()it looks like code from micro suckers
274 it()(which) might scare away some (otherwise good?)
275     progammers, or make you a paria in the free
276     software community
277 it()it has ambiguities
278 it()not very useful if not used consistently
279 it()usefullness in em(very large) (but how many classes is very large?)
280     remains an issue.
281 )
282
283 nsubsect(Proposal)
284
285 itemize(
286 it()learn about cut and paste / use emacs or vi
287     or lean to type using ten fingers
288 it()Use emacs dabbrev-expand, with dabbrev-case-fold-search set to nil.
289 it()use no, or pick less silly, abbrvs.
290 it()use non-ambiguous postfixes code(identifier_name_type_modifier[_modifier])
291 it()There is no need for Hungarian if the scope of the variable is small,
292     ie. local variables, arguments in function definitions (not
293     declarations).
294 )
295
296 Macros, code(enum)s and code(const)s are all uppercase,
297 with the parts of the names separated by underscores.
298
299 nsubsect(Types)
300
301 description(
302 dit(code(byte))
303     unsigned char. (The postfix _by is ambiguous)
304 dit(code(b))
305     bool
306 dit(code(bi))
307     bit
308 dit(code(ch))
309     char
310 dit(code(f))
311     float
312 dit(code(i))
313     signed integer
314 dit(code(str))
315     string class
316 dit(code(sz))
317     Zero terminated c string
318 dit(code(u))
319     unsigned integer
320 )
321
322 nsubsect(User defined types)
323
324 verb(
325         /** Slur blah. blah.
326         (slur)
327         */
328         class Slur {};
329         Slur* slur_p = new Slur;
330 )
331
332 nsubsect(Modifiers)
333
334 The following types modify the meaning of the prefix. 
335 These are preceded by the prefixes:
336
337 description(
338 dit(code(a))
339     array
340 dit(code(array))
341     user built array.
342 dit(code(c))
343     const. Note that the proper order is code(Type const)
344     i.s.o. code(const Type)
345 dit(code(C))
346     A const pointer. This would be equivalent to code(_c_l), but since any
347     "const" pointer has to be a link (you can't delete a const pointer),
348     it is superfluous.
349 dit(code(l))
350     temporary pointer to object (link)
351 dit(code(p))
352     pointer to newed object
353 dit(code(r))
354     reference
355 )
356
357 nsubsect(Adjective)
358
359 Adjectives such as global and static should be spelled out in full.
360 They come before the noun that they refer to, just as in normal english.
361
362 verb(
363 foo_global_i: a global variable of type int commonly called "foo".
364 )
365
366 static class members do not need the static_ prefix in the name (the
367 Class::var notation usually makes it clear that it is static)
368
369 description(
370 dit(code(loop_i))
371     Variable loop: an integer
372 dit(code(u))
373     Temporary variable: an unsigned integer
374 dit(code(test_ch))
375     Variable test: a character
376 dit(code(first_name_str))
377     Variable first_name: a String class object
378 dit(code(last_name_ch_a))
379     Variable last_name: a code(char) array
380 dit(code(foo_i_p))
381     Variable foo: an code(Int*) that you must delete
382 dit(code(bar_i_l))
383     Variable bar: an code(Int*) that you must not delete
384 )
385
386 Generally default arguments are taboo, except for nil pointers.
387
388 The naming convention can be quite conveniently memorised, by
389 expressing the type in english, and abbreviating it
390
391 verb(
392         static Array<int*> foo
393 )
394
395 code(foo) can be described as "the static int-pointer user-array", so you get
396
397 verb(
398         foo_static_l_arr
399 )
400
401
402
403 nsect(MISCELLANEOUS)
404
405 For some tasks, some scripts are supplied, notably creating patches, a
406 mirror of the website, generating the header to put over cc and hh
407 files, doing a release.
408
409 Use them.
410
411 The following generic identifications are used:
412
413 verb(
414         up == 1
415         left == -1
416         right == 1
417         down == -1
418 )
419
420 Intervals are pictured lying on a horizontal numberline (Interval[-1]
421 is the minimum). The 2D plane has +x on the right, +y pointing up.
422
423