]> git.donarmstrong.com Git - lilypond.git/blobdiff - Documentation/CodingStyle.pod
partial: 1.0.1.jcn
[lilypond.git] / Documentation / CodingStyle.pod
index 2f65fb775ebbc3235b9aa8b2d1fddc66965bbb2b..0f57d3e1b31bd48a60b019cf6bd519bad4a50105 100644 (file)
@@ -1,10 +1,12 @@
 =head1 NAME
 
-CodingStyle - standards while programming for LilyPond
+CodingStyle - standards while programming for GNU LilyPond
 
 =head1 DESCRIPTION
 
-Please use these standards while doing programming for LilyPond
+We use these standards while doing programming for GNU LilyPond.  If
+you do some hacking, we appreciate it if you would follow this rules,
+but if you don't, we still like you.
 
 Functions and methods do not return errorcodes, but use assert for
 checking status. 
@@ -12,7 +14,7 @@ checking status.
 =head2 Quote:
 
 A program should be light and agile, its subroutines
-connected like a strings of pearls.  The spirit and intent of
+connected like a string of pearls.  The spirit and intent of
 the program should be retained throughout.  There should be
 neither too little nor too much, neither needless loops nor
 useless variables, neither lack of structure nor overwhelming
@@ -33,33 +35,87 @@ this is to rewrite the program.
 
 -- Geoffrey James, "The Tao of Programming"
 
-=head2 INDENTATION
+
+=head2 LANGUAGES
+
+C++, /bin/sh and python are preferred.  Perl is not.
+
+=head2 FILES
+
+Definitions of classes that are only accessed via pointers
+(*) or references (&) shall not be included as include files.
+
+filenames
+
+       ".hh"   Include files
+       ".cc"   Implementation files
+       ".icc"  Inline definition files
+       ".tcc"  non inline Template defs
 
 in emacs:
 
+       (setq auto-mode-alist
+             (append '(("\\.make$" . makefile-mode)
+                       ("\\.cc$" . c++-mode)
+                       ("\\.icc$" . c++-mode)
+                       ("\\.tcc$" . c++-mode)
+                       ("\\.hh$" . c++-mode)
+                       ("\\.pod$" . text-mode)         
+                       )
+                     auto-mode-alist))
+
 
-       (add-hook 'c-mode-hook
-                 '(lambda ()(setq c-basic-offset 4)))
+The class Class_name_abbreviation is coded in F<class-name-abbr.*>
+
+
+=head2 INDENTATION
+
+in emacs:
 
 
        (add-hook 'c++-mode-hook
-                 '(lambda() (c-set-style "Stroustrup")
+                 '(lambda() (c-set-style "gnu")
                     )
                  )
 
+If you like using font-lock, you can also add this to your F<.emacs>:
+
+       (setq font-lock-maximum-decoration t)
+       (setq c++-font-lock-keywords-3 
+             (append
+              c++-font-lock-keywords-3
+              '(("\\b\\([a-zA-Z_]+_\\)\\b" 1 font-lock-variable-name-face)
+              ("\\b\\([A-Z]+[a-z_]+\\)\\b" 1 font-lock-type-face))
+              ))
 
 =head2 CLASSES and TYPES:
 
        This_is_a_class
        AClass_name     (for Abbreviation_class_name)
 
-=head2 DATA MEMBERS
+=head2 MEMBERS
 
        Class::member()
        Type Class::member_type_
+       Type Class::member_type()
+
+the C<type> is a Hungarian notation postfix for C<Type>. See below
 
-the C<type> is a Hungarian notation postfix for $C<Type>$. See below
+=head2 MACROS
 
+Macros should be written completely in uppercase
+
+The code should not be compilable if proper macro declarations are not
+included. 
+
+Don't laugh. It took us a whole evening/night to figure out one of
+these bugs.
+
+=head2 BROKEN CODE
+
+Broken code (hardwired dependencies, hardwired constants, slow
+algorithms and obvious limitations) should be marked as such:
+either with a verbose TODO, or with a short "ugh" comment.
 
 =head2 COMMENTS
 
@@ -68,6 +124,7 @@ http://www.zib.de/Visual/software/doc++/index.html
 
        /*
                C style comments for multiline comments.
+               They come before the thing to document.
                [...]
        */
 
@@ -76,6 +133,8 @@ http://www.zib.de/Visual/software/doc++/index.html
                short description.
                Long class documentation.
                (Hungarian postfix)
+
+               TODO Fix boring_member()
        */
        class Class {
                /**
@@ -94,22 +153,14 @@ http://www.zib.de/Visual/software/doc++/index.html
                Rettype member(Argtype);
 
                /// memo only
-               boring_member();
+               boring_member() {
+                       data_member_ = 121; // ugh
+               }
        };
 
-Unfortunately most of the code isn't really documented that good.
-
 
-=head2 CLASSNAMES (2)
-
-A lot of classes in LilyPond start with 'P', this is to distinguish
-certain parts of LilyPond: the P stands for Printer, and the P-classes
-are supposed to be more lowlevel than the others. Example:
-
-Staff uses PStaff, PScore and PCol to do the typesetting of
-symbols. Staff is  the "brains" for PStaff
-
-NB: in PCursor (which is part of the library) P stands for PointerCursor
+       
+Unfortunately most of the code isn't really documented that good.
 
 
 =head2 MEMBERS (2)
@@ -131,10 +182,28 @@ Standard methods:
        Presence of these methods usually imply that it is not feasible to this
        via  a constructor
        */
-       add( .. )
+       add (..)
 
        /// replace some data of *this
-       set( .. )
+       set (..)
+
+=head2 Constructor
+
+Every class should have a default constructor.  
+
+Don't use non-default constructors if this can be avoided:
+
+       Foo f(1)
+
+is less readable than
+
+       Foo f;
+       f.x = 1
+
+or 
+       Foo f(Foo_convert::int_to_foo (1))
+
+
 
 =head1 HUNGARIAN NOTATION NAMING CONVENTION
 
@@ -169,10 +238,11 @@ Another fun quote from Microsoft Secrets:
        our source code, you also notice very few comments.  Hungarian
        gives us the ability to go in and read code..."
 
-
 Wow! If you use Hungarian you don't have to document your software!
 Just think of the hours I have wasted documenting while this "silver bullet"
-existed. I feel so stupid and ashamed!
+existed. I feel so stupid and ashamed!  [Didn't MMM-Brooks say `There is
+no silver bullet?' --HWN]
+
 
 =head2 Disadvantages
 
@@ -206,8 +276,7 @@ not very useful if not used consistently
 
 =item *
 
-usefullness in I<very large> 
-(but how many classes is very large?)
+usefullness in I<very large> (but how many classes is very large?)
 remains an issue.
 
 =back
@@ -234,6 +303,13 @@ use no, or pick less silly, abbrvs.
 =item *
 
 use non-ambiguous postfixes C<identifier_name_type_modifier[_modifier]>
+
+=item *
+
+There is no need for Hungarian if the scope of the variable is small,
+ie. local variables, arguments in function definitions (not
+declarations).
+
 =back
 
 Macros, C<enum>s and C<const>s are all uppercase,
@@ -295,7 +371,7 @@ unsigned integer
 =head2 Modifiers
 
 The following types modify the meaning of the prefix. 
-These are precede the prefixes:
+These are preceded by the prefixes:
 
 =over 5
 
@@ -309,7 +385,7 @@ user built array.
 
 =item C<c>
 
-const. Note that the proper order C<Type const> i.s.o. C<const Type>
+const. Note that the proper order is C<Type const> i.s.o. C<const Type>
 
 =item C<C>
 
@@ -333,6 +409,16 @@ reference
 
 =over 5
 
+=head2 Adjective
+
+Adjectives such as global and static should be spelled out in full.
+They come before the noun that they refer to, just as in normal english.
+
+foo_global_i: a global variable of type int commonly called "foo".
+
+static class members do not need the static_ prefix in the name (the
+Class::var notation usually makes it clear that it is static)
+
 =item C<loop_i>
 
 Variable loop: an integer
@@ -363,5 +449,36 @@ Variable bar: an C<Int*> that you must not delete
 
 =back
 
+Generally default arguments are taboo, except for nil pointers.
+
+The naming convention can be quite conveniently memorised, by
+expressing the type in english, and abbreviating it
+
+       static Array<int*> foo
+
+C<foo> can be described as "the static int-pointer user-array", so you get
+
+       foo_static_l_arr
+       
+
+
+
+=head1 MISCELLANEOUS
+
+For some tasks, some scripts are supplied, notably creating patches, a
+mirror of the website, generating the header to put over cc and hh
+files, doing a release.
+
+Use them.
+
+The following generic identifications are used:
+
+       up == 1
+       left == -1
+       right == 1
+       down == -1
+
+Intervals are pictured lying on a horizontal numberline (Interval[-1]
+is the minimum). The 2D plane has +x on the right, +y pointing up.