Difference between revisions of "CodingConventions"From Globulation2
Revision as of 04:37, 24 July 2007ContentsCoding ConventionsThis page lists the coding conventions actually in use within Glob2. The intended purpose is to have coherent-looking code through the project. Sorry if this different to your usual conventions. KISS: Keep it Simple StupidAll new code written in Glob2 should be obvious. Don't use clever tricks. Everything should be kept as simple as possible, but no simpler. This is a fundamental principle, and all new code should be kept with KISS in mind. Maintainability Is GodIf code is readable, if its maintainable, then it is good. Sometimes people get haggled about using pure OO or get/set. Make sure that your code is maintainable. If you change one portion, always try to minimize the amount of other code segments that need updated. This encompasses many techniques. Encapsulation is one of the best. IndentationCode should be indented by tabs. A tab should be equivalent to 4 spaces. Please note that in the code below, non-breaking spaces are used because tabs don't display correctly in web pages. Class and struct namingClasses should begin with capitals, new words should be denoted by a capital, and words should not be separated by underscores or dashes. In other words, class TotoIsMyFriend { }; // correct
instead of: class toto_is_my_friend{}; // wrong
Class members, variables, and C function follow the same convention but without the initial capital: void totoIsMyFriend(void); // correct
instead of: void Toto-is-my-friend(void); // wrong
Bracket placementBrackets {} should be placed alone on a line, except if there is no or only one function call within them. class Toto
{
int i;
void totoIsMyFriend(void) { return i; };
}; // correct
instead of: class Toto {
int i;
void Toto-is-my-friend(void) {
return i;
};
}; // wrong
EmacsIf you use Emacs as your editor, you can make it apply these rules to Globulation 2 files by putting the following in the .emacs file in your home directory: (defconst glob2-c-style '((c-basic-offset . 4) (c-offsets-alist . ((substatement-open . 0) (inline-open . 0) )) ) "Globulation 2 programming style") ;; Customizations for all modes in CC Mode. (defun glob2-c-mode-common-hook () (c-add-style "glob2" glob2-c-style) ) (add-hook 'c-mode-common-hook 'glob2-c-mode-common-hook) And adding the following to the top line of each of your source files: /* emacs settings information: -*- mode: c++; tab-width: 4; c-file-style: "glob2"; -*- */ Proposed ConventionsThis section discusses proposals for programming style rules that have not yet been widely accepted CountingWhen referring to the total number of items of a particular type that there are, the current rule is to use
By extension, where there is an ordered list of things (most likely an enum), we should always have the following definitions: enum foo { foo1, foo2, ... fooN }; static const int fooMin = foo1; static const int fooMax = fooN; static const int fooCount = fooMax - fooMin + 1; Getting and settingAt present, it's standard to get values with | ||||||||||||||||||||||||||||||