had this hanging around...

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@24919 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2008-05-23 19:39:56 +00:00
parent 225e9e5547
commit fae43b38d0
2 changed files with 51 additions and 42 deletions

View File

@ -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 rules Meyers presents, only the most important of them. LyX does not yet
follow these rules, but they should be the goal. 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 i.e. write delete[] obj; if new obj[n]; was used to create
the object and write delete obj; if you wrote new obj; the object and write delete obj; if you wrote new obj;
Notice strings should be std::string's instead of char *'s. 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 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 - strive for class interfaces that are complete and minimal
- differentiate among member functions, global functions and friend - 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 - use const whenever possible
- pass and return objects by reference instead of by value - pass and return objects by reference instead of by value
- choose carefully between function overloading and - choose carefully between function overloading and
parameter defaulting. parameter defaulting
- never return a reference to a local object or a dereferenced - 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 - pay attention to compiler warnings
- differentiate between inheritance of interface and - differentiate between inheritance of interface and
inheritance of implementation. inheritance of implementation
- differentiate between inheritance and templates - 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 - 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) 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') And one of mine: (Andre')
- try to implement your class in a way that the automatically generated - 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

View File

@ -2,8 +2,8 @@ Rules for the code in LyX
------------------------- -------------------------
[updated from the C++STYLE distributed with the GNU C++ Standard] [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 The aim of this file is to serve as a guide for the developers, to aid
get clean and uniform code. This document is still incomplete. us to get clean and uniform code. This document is incomplete.
We really like to have new developers joining the LyX Project. However, We really like to have new developers joining the LyX Project. However,
we have had problems in the past with developers leaving the we have had problems in the past with developers leaving the
@ -95,8 +95,8 @@ in C++.
++T; ++T;
--U; --U;
-NOT- -NOT-
T++; // wrong T++; // not used in LyX
U--; // wrong U--; // not used in LyX
- Try to minimize evaluation of the same code over and over. This is - Try to minimize evaluation of the same code over and over. This is
aimed especially at loops. aimed especially at loops.
@ -138,11 +138,10 @@ in C++.
Exceptions Exceptions
---------- ----------
Even if LyX currently is not using exceptions we need to be aware of Be aware of the presence of exceptions. One important thing to realize
them. One important thing to realize is that you often do not have to is that you often do not have to use throw, try or catch to be exception
use throw, try or catch to be exception safe. Let's look at the safe. Let's look at the different types of exceptions safety: (These are
different types of exceptions safety: (These are taken from Herb taken from Herb Sutter's book[ExC++]
Sutter's book[ExC++]
" "
1. Basic guarantee: Even in the presence of exceptions thrown by T or 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 should look over all destructors to ensure that they are as exception
safe as possible. 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 Formatting
---------- ----------
@ -207,13 +201,13 @@ Formatting
int a; int a;
int b; int b;
-NOT- -NOT-
int a, b; // wrong int a, b; // not used in LyX
This is especially important when initialization is done at the same This is especially important when initialization is done at the same
time: time:
string a = "Lars"; string a = "Lars";
string b = "Gullik"; string b = "Gullik";
-NOT- -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 [Note that 'string a = "Lars"' is formally calling a copy constructor
on a temporary constructed from a string literal and therefore has the on a temporary constructed from a string literal and therefore has the
@ -230,20 +224,20 @@ Formatting
char * p = "flop"; char * p = "flop";
char & c = *p; char & c = *p;
-NOT- -NOT-
char *p = "flop"; // wrong char *p = "flop"; // not used in LyX
char &c = *p; // wrong char &c = *p; // not used in LyX
Some time ago we had a huge discussion on this subject and after Some time ago we had a huge discussion on this subject and after
convincing argumentation from Asger this is what we decided. Also note convincing argumentation from Asger this is what we decided. Also note
that we will have: that we will have:
char const * p; char const * p;
-NOT- -NOT-
const char * p; // wrong const char * p; // not used in LyX
* Operator names and parentheses * Operator names and parentheses
operator==(type) operator==(type)
-NOT- -NOT-
operator == (type) // wrong operator == (type) // not used in LyX
The == is part of the function name, separating it makes the The == is part of the function name, separating it makes the
declaration look like an expression. declaration look like an expression.
@ -251,7 +245,7 @@ Formatting
* Function names and parentheses * Function names and parentheses
void mangle() void mangle()
-NOT- -NOT-
void mangle () // wrong void mangle () // not used in LyX
* Enumerators * Enumerators
enum Foo { enum Foo {
@ -260,7 +254,7 @@ Formatting
FOO_THREE = 3 FOO_THREE = 3
}; };
-NOT- -NOT-
enum { one = 1, two = 2, three 3 }; // wrong enum { one = 1, two = 2, three 3 }; // not used in LyX
-NOT- -NOT-
enum { enum {
One = 1, One = 1,
@ -268,6 +262,21 @@ Formatting
Three = 3 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 * Naming rules for classes
- Use descriptive but simple and short names. Do not abbreviate. - 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. - Avoid declaring global objects in the declaration file of the class.
If the same variable is used for all objects, use a static member. If the same variable is used for all objects, use a static member.
- Avoid global or static variables. An exception to this rule is - Avoid global or static variables.
very private stuff like the math stack.
* File headers * File headers