]> git.lyx.org Git - lyx.git/blob - development/Code_rules/Rules
ffc74c8d689d12dc97d96864c94b1777df3d1709
[lyx.git] / development / Code_rules / Rules
1 Rules for the code in LyX
2 -------------------------
3 [updated from the C++STYLE distrubuted with the GNU C++ Standard]
4
5 The aim of this file is to serve as a guide for the developers, to aid us to
6 get clean and uniform code. Still uncomplete.
7
8 We really like to have new developers joining the LyX Project. However
9 since we have had problems in the past with developers leaving the
10 project and their contributed code in a far from perfect state. Most
11 of this happened before that we really became aware of these issues,
12 but still, we don't want it to happen again. So we have put together
13 some guidelines and rules for the developers.
14
15 In general, if you want to contribute to the main source, we expect at least 
16 that you:
17
18 - write good C++ code: Readable, well commented and taking advantage of the 
19   OO model.
20 - adapt the code to the structures already existing in LyX, or in case that 
21   you have better ideas, discuss them on the developer's list before writing 
22   the code.
23 - take advantage of the C++ standard library.
24
25 These guidelines should save us a lot of work while cleaning up the code and 
26 help us to have quality code. LyX has been haunted by problems coming from 
27 unfinished projects by people who have left the team. Those problems will 
28 hopefully disappear if the code is easy to hand over to somebody else.
29
30 When you send in a patch or commit to the LyX cvs repository we expect
31 you to add a ChangeLog entry. The entry should have this syntax:
32
33 1999-12-13  Lars Gullik Bjønnes  <larsbj@lyx.org>
34
35         * src/support/lyxstring.C (find): assert bug fixed.
36
37 * Pointers and references
38   char * p = "flop";
39   char & c = *p;
40       -NOT-
41   char *p = "flop"; // wrong
42   char &c = *p;     // wrong
43
44  Some time ago we had a huge discusion on this subject and after
45 convincing argumentation from Asger this is what we decided. Also note
46 that we will have:
47  char const * p;
48       -NOT-
49  const char * p;
50
51 * Operator names and parentheses
52   operator==(type)
53        -NOT-
54   operator == (type)  // wrong
55
56   The == is part of the function name, separating it makes the
57 declaration look like an expression.
58
59 * Function names and parentheses
60   void mangle()
61        -NOT-
62   void mangle ()  // wrong
63
64 * Enumerators
65   enum {
66         one = 1,
67         two = 2,
68         three = 3
69   };
70   -NOT-
71   enum { one = 1, two = 2, three 3 };
72
73 * Naming rules for classes
74
75   - Use descriptive but simple and short names. For stuff specific to LyX
76     use LyX as prefix. Some modules, like mathed or spellchecker, could have
77     other prefixes.
78     [I am not so sure about the LyX prefix]
79
80   - Class names are usually capitalized, and function names lowercased.
81     Enums are named like Classes, enum values in CAPS.
82
83   - Long variables are named like thisLongVariableName.
84
85   New types are capitalized, so this goes for typedefs,classes,structs
86 and enums.
87
88 * Formatting
89
90   - Please adapt the formatting of your code to the setting in LyX in that
91     particular file. Lars and Asger are slowly, but surely moving the source 
92     towards Linux kernel style formatting, aka K&R style. We suggest that you 
93     also do this, but this is NOT something that has been decided generally.
94
95
96 * Use existing structures
97
98   - Use string whereever possible. LyX will someday move to Unicode, and
99     that will be easy if everybody uses string now.
100
101   - Check out the filename and path tools in filetools.h
102
103   - Check out the string tools in lstring.h, and the SubString class
104     and the regex class.
105
106   - Use the DebugStream class to report errors and messages using
107     the lyxerr instantation.
108
109   [add description of other existing structures]
110
111
112 * Declarations
113   
114   - Use this order for the access sections of your class: public,
115     protected, private. The public section is interesting for every
116     user of the class. The private section is only of interest for the
117     implementors of the class (you). [Obvously not true since this is
118     for developers, and we do not want one developer only to be able to
119     read and understand the implementation of class internals. Lgb]
120   
121   - Avoid to declare global objects in the declaration file of the class. 
122     If the same variable is used for all object, use a static member.
123
124   - Avoid global or static variables. An exception to this rule is 
125     very private stuff like the math stack.
126
127   - Use the const keyword like this: char const * instead of const char *
128     because this is more logical.
129
130
131 * Documentation
132
133   - The documentation is generated from the header files.
134   - You document for the other developers, not for yourself.
135   - You should document what the funtion do, not the implementation.
136   - in the .C files you document the implementation.
137   - Single line description (///), multiple lines description (/** ... */)
138   - You make the documentation by doing "make srcdoc" in the root,
139     and then you'll find HTML in the srcdoc/ directory. Read with
140     Netscape for best results.
141
142
143 * NAMING RULES FOR USER-COMMANDS
144    
145   Here's the set of rules to apply when a new command name is introduced:
146  
147   1) Use the object.event order. That is, use `word-forward' instead of 
148      `forward-word'.
149   2) Don't introduce an alias for an already named object. Same for events.
150   3) Forward movement or focus is called `forward' (not `right').
151   4) Backward movement or focus is called `backward' (not `left').
152   5) Upward movement of focus is called `up'.
153   6) Downward movement is called `down'.
154   7) The begin of an object is called `begin' (not `start').
155   8) The end of an object is called `end'.
156
157
158 * Using external GUI constructors (XForms fdesign)
159
160   - Fdesign generated files should not be changed at all. The only changes
161     needed are gettext, compability with 0.81 or when you have made your own
162     xforms objects and have just a dummy in the .fd file in place of your
163     own. In case you have to change the generated files for any of the
164     reasons above, you should provide a patch against the clean generated
165     file. Your callbacks must be in a separate file.