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