| | 1 | (Note: This document is aimed at C++ code. We should write something similar for JS.) |
| | 2 | |
| | 3 | == Introduction == |
| | 4 | |
| | 5 | The goal of these coding conventions is to encourage consistency within the code, rather than claiming some particular style is better than any other. As such, the main guideline is: |
| | 6 | * When modifying a piece of code, try to follow its existing style. |
| | 7 | Our code is currently self-inconsistent in places, but the following guidelines attempt to describe the most common style and are what we should converge towards. Obviously we want clean, readable, adequately-documented code - lots of articles and books already talk about how to do that - so we mostly describe boring typographic details. |
| | 8 | |
| | 9 | == Brief guidelines == |
| | 10 | |
| | 11 | * All source files (.cpp, .h) must start with the following header, before any other content: |
| | 12 | {{{ |
| | 13 | /* Copyright (C) 2009 Wildfire Games. |
| | 14 | * This file is part of 0 A.D. |
| | 15 | * |
| | 16 | * 0 A.D. is free software: you can redistribute it and/or modify |
| | 17 | * it under the terms of the GNU General Public License as published by |
| | 18 | * the Free Software Foundation, either version 2 of the License, or |
| | 19 | * (at your option) any later version. |
| | 20 | * |
| | 21 | * 0 A.D. is distributed in the hope that it will be useful, |
| | 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| | 24 | * GNU General Public License for more details. |
| | 25 | * |
| | 26 | * You should have received a copy of the GNU General Public License |
| | 27 | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. |
| | 28 | */ |
| | 29 | }}} |
| | 30 | replacing `2009` with the year that the file was last updated. |
| | 31 | |
| | 32 | * Wrap header files in include guards, using the name `INCLUDED_`''filename'', e.g. the file `Foo.h` should say: |
| | 33 | {{{ |
| | 34 | #ifndef INCLUDED_FOO |
| | 35 | #define INCLUDED_FOO |
| | 36 | ... |
| | 37 | #endif // INCLUDED_FOO |
| | 38 | }}} |
| | 39 | |
| | 40 | * The first non-comment line of any source file must be `#include "precompiled.h"` |
| | 41 | |
| | 42 | * In header files, avoid `#include` and use forward declarations wherever possible. |
| | 43 | |
| | 44 | * Use tabs for indentation, not spaces. |
| | 45 | |
| | 46 | * Indent braces like |
| | 47 | {{{ |
| | 48 | int CExampleObject::DoSomething(int value) |
| | 49 | { |
| | 50 | if (value != 0) |
| | 51 | { |
| | 52 | Prepare(); |
| | 53 | m_Value = value; |
| | 54 | } |
| | 55 | return value; |
| | 56 | } |
| | 57 | }}} |
| | 58 | |
| | 59 | * Class names are !CamelCase and prefixed with `C`, e.g. `CGameObject`. Member functions are !CamelCase, e.g. `CGameObject::SetModifiedFlag(...)`. Member variables are !CamelCase prefixed with `m_`, e.g. `CGameObject::m_ModifiedFlag`. Files are named `GameObject.cpp`, `GameObject.h`, usually with one major class per file (possibly with some other support classes in the same files). |
| | 60 | |
| | 61 | * Use [http://www.cppdoc.com/ CppDoc] comments, e.g. |
| | 62 | {{{ |
| | 63 | class CExampleObject |
| | 64 | { |
| | 65 | /** |
| | 66 | * Sets the object's current value to the passed value, if it's non-zero. |
| | 67 | * |
| | 68 | * @param v the new value to set it to, or 0 to do nothing. |
| | 69 | * |
| | 70 | * @return the value that was passed in. |
| | 71 | */ |
| | 72 | int DoSomething(int v); |
| | 73 | }; |
| | 74 | }}} |
| | 75 | |
| | 76 | * Use `CStr` instead of `std::string`. Use `CStrW` instead of `std::wstring`. |
| | 77 | |
| | 78 | * Use STL when appropriate. |
| | 79 | |
| | 80 | * ... |
| | 81 | |
| | 82 | = Old document = |
| | 83 | |
| | 84 | ''The following content might be somewhat incorrect or outdated, but it's preserved here since it contains some useful information.'' |
| | 85 | |