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