diff --git a/development/coding/Recommendations b/development/coding/Recommendations index 4680beafa7..86053215a4 100644 --- a/development/coding/Recommendations +++ b/development/coding/Recommendations @@ -3,52 +3,53 @@ Scott Meyers, and are presented in their short form. These are not all the rules Meyers presents, only the most important of them. LyX does not yet follow these rules, but they should be the goal. -- Use const and inline instead of #define +- use const and inline instead of #define -- Use the same form in corresponding calls to new and delete, +- use the same form in corresponding calls to new and delete, i.e. write delete[] obj; if new obj[n]; was used to create the object and write delete obj; if you wrote new obj; Notice strings should be std::string's instead of char *'s. -- Define a default constructor, copy constructor and an assignment +- define a default constructor, copy constructor and an assignment operator for all classes with dynamically allocated memory that - do not inherit noncopyable + are not made noncopyable -- make destructors virtual in base classes and only there. +- do not define default constructor, copy constructor and an assignment + operator if the compiler generated one would do the same -- assign to all data members in operator=. +- make destructors virtual in base classes and only there + +- assign to all data members in operator=() - strive for class interfaces that are complete and minimal - differentiate among member functions, global functions and friend - functions. + functions -- avoid data members in the public interface. +- avoid data members in the public interface - use const whenever possible - pass and return objects by reference instead of by value - choose carefully between function overloading and - parameter defaulting. + parameter defaulting - never return a reference to a local object or a dereferenced - pointer initialized by new within the function. + pointer initialized by new within the function -- use enums for integral constants. +- use enums for integral constants -- minimize compilation dependencies between files. +- minimize compilation dependencies between files - pay attention to compiler warnings - differentiate between inheritance of interface and - inheritance of implementation. + inheritance of implementation - differentiate between inheritance and templates -- know what functions C++ silently writes and calls. - -- ensure that global objects are initialized before they are used. +- ensure that global objects are initialized before they are used - avoid conditions to 'if' and 'while' that span more than a line @@ -61,11 +62,11 @@ Design. Addison-Wesley, 1992 And one of mine: (Lgb) -- When swiching on enums, refrain from using "default:" if possible. +- when swiching on enums, refrain from using "default:" if possible And one of mine: (Andre') - try to implement your class in a way that the automatically generated - copy constructor and copy assignment work out-of-the box. + copy constructor and copy assignment work out-of-the box diff --git a/development/coding/Rules b/development/coding/Rules index f2a1b6e140..68846a668f 100644 --- a/development/coding/Rules +++ b/development/coding/Rules @@ -2,8 +2,8 @@ Rules for the code in LyX ------------------------- [updated from the C++STYLE distributed with the GNU C++ Standard] -The aim of this file is to serve as a guide for the developers, to aid us to -get clean and uniform code. This document is still incomplete. +The aim of this file is to serve as a guide for the developers, to aid +us to get clean and uniform code. This document is incomplete. We really like to have new developers joining the LyX Project. However, we have had problems in the past with developers leaving the @@ -95,8 +95,8 @@ in C++. ++T; --U; -NOT- - T++; // wrong - U--; // wrong + T++; // not used in LyX + U--; // not used in LyX - Try to minimize evaluation of the same code over and over. This is aimed especially at loops. @@ -138,11 +138,10 @@ in C++. Exceptions ---------- -Even if LyX currently is not using exceptions we need to be aware of -them. One important thing to realize is that you often do not have to -use throw, try or catch to be exception safe. Let's look at the -different types of exceptions safety: (These are taken from Herb -Sutter's book[ExC++] +Be aware of the presence of exceptions. One important thing to realize +is that you often do not have to use throw, try or catch to be exception +safe. Let's look at the different types of exceptions safety: (These are +taken from Herb Sutter's book[ExC++] " 1. Basic guarantee: Even in the presence of exceptions thrown by T or @@ -194,11 +193,6 @@ without using try, throw or catch we should do so. In particular we should look over all destructors to ensure that they are as exception safe as possible. -Later when more compiler support exceptions sufficiently well we will -begin using them too. One reason for this is that the C++ standard -library actually requires exceptions, e.g. "new" will throw -bad_allocation if the requested memory is not available. - Formatting ---------- @@ -207,13 +201,13 @@ Formatting int a; int b; -NOT- - int a, b; // wrong + int a, b; // not used in LyX This is especially important when initialization is done at the same time: string a = "Lars"; string b = "Gullik"; -NOT- - string a = "Lars", b = "Gullik"; // wrong + string a = "Lars", b = "Gullik"; // not used in LyX [Note that 'string a = "Lars"' is formally calling a copy constructor on a temporary constructed from a string literal and therefore has the @@ -230,20 +224,20 @@ Formatting char * p = "flop"; char & c = *p; -NOT- - char *p = "flop"; // wrong - char &c = *p; // wrong + char *p = "flop"; // not used in LyX + char &c = *p; // not used in LyX Some time ago we had a huge discussion on this subject and after convincing argumentation from Asger this is what we decided. Also note that we will have: char const * p; -NOT- - const char * p; // wrong + const char * p; // not used in LyX * Operator names and parentheses operator==(type) -NOT- - operator == (type) // wrong + operator == (type) // not used in LyX The == is part of the function name, separating it makes the declaration look like an expression. @@ -251,7 +245,7 @@ Formatting * Function names and parentheses void mangle() -NOT- - void mangle () // wrong + void mangle () // not used in LyX * Enumerators enum Foo { @@ -260,7 +254,7 @@ Formatting FOO_THREE = 3 }; -NOT- - enum { one = 1, two = 2, three 3 }; // wrong + enum { one = 1, two = 2, three 3 }; // not used in LyX -NOT- enum { One = 1, @@ -268,6 +262,21 @@ Formatting Three = 3 }; +* Null pointers + + Using a plain 0 is always correct and least effort to type. So: + + void * p = 0; + -NOT- + void * p = NULL; // not used in LyX + -NOT- + void * p = '\0'; // not used in LyX + -NOT- + void * p = 42 - 7 * 6; // not used in LyX + + Note: As an exception, imported third party code as well as code + interfacing the "native" APIs (src/support/os_*) can use NULL. + * Naming rules for classes - Use descriptive but simple and short names. Do not abbreviate. @@ -314,8 +323,7 @@ Formatting - Avoid declaring global objects in the declaration file of the class. If the same variable is used for all objects, use a static member. - - Avoid global or static variables. An exception to this rule is - very private stuff like the math stack. + - Avoid global or static variables. * File headers