]> git.lyx.org Git - lyx.git/blob - development/coding/Recommendations
avoid float-conversion warning and simplify size computation
[lyx.git] / development / coding / Recommendations
1 These are some rules for effective C++ programming. These are taken from
2 Scott Meyers, and are presented in their short form. These are not all the
3 rules Meyers presents, only the most important of them. LyX does not yet
4 follow these rules, but they should be the goal.
5
6 - use const and inline instead of #define
7
8 - use the same form in corresponding calls to new and delete,
9   i.e. write delete[] obj; if new obj[n]; was used to create
10   the object and write delete obj; if you wrote new obj;
11   Notice strings should be std::string's instead of char *'s.
12
13 - define a default constructor, copy constructor and an assignment
14   operator for all classes with dynamically allocated memory that
15   are not made noncopyable
16
17 - do not define default constructor, copy constructor and an assignment
18   operator if the compiler generated one would do the same
19
20 - make destructors virtual in base classes and only there
21
22 - assign to all data members in operator=()
23
24 - strive for class interfaces that are complete and minimal
25
26 - differentiate among member functions, global functions and friend
27   functions
28
29 - avoid data members in the public interface
30
31 - use const whenever possible
32
33 - pass and return objects by reference instead of by value
34
35 - choose carefully between function overloading and
36   parameter defaulting
37
38 - never return a reference to a local object or a dereferenced
39   pointer initialized by new within the function
40
41 - use enums for integral constants
42
43 - minimize compilation dependencies between files
44
45 - pay attention to compiler warnings
46
47 - differentiate between inheritance of interface and
48   inheritance of implementation
49
50 - differentiate between inheritance and templates
51
52 - ensure that global objects are initialized before they are used
53
54 - avoid conditions to 'if' and 'while' that span more than a line
55
56 --------
57
58 S. Meyers. Effective C++, 50 Specific Ways to Improve Your Programs and
59 Design. Addison-Wesley, 1992
60
61 ==================================
62
63 And one of mine: (Lgb)
64
65 - when swiching on enums, refrain from using "default:" if possible
66
67
68 And one of mine: (Andre')
69
70 - try to implement your class in a way that the automatically generated
71   copy constructor and copy assignment work out-of-the box
72