Changes between Version 7 and Version 8 of Coding_Conventions


Ignore:
Timestamp:
2011-11-24 13:19:29 (18 months ago)
Author:
Philip
Comment:

more conventions

Legend:

Unmodified
Added
Removed
Modified
  • Coding_Conventions

    v7 v8  
    9696 * Try to avoid very wide lines. Typical wrapping points are 80 characters, or 120, or 132, etc. (There's no strict limit - aim for whatever seems most readable.) 
    9797 
     98 * Write switch statements like 
     99{{{ 
     100switch (n) 
     101{ 
     102case 10: 
     103    return 1; 
     104 
     105case 20: 
     106    foo(); 
     107    // fall through to next case    [this should be explicit if you don't end with break or return] 
     108 
     109case 30: 
     110    bar(); 
     111    break; 
     112 
     113case 40: 
     114{ 
     115    int i = n*2;       // [need the extra {...} scope when declaring variables inside the case] 
     116    return i; 
     117} 
     118 
     119default: 
     120    debug_warn(L"invalid value for n");   // [only do this kind of warning if this case is an engine bug] 
     121} 
     122 
     123}}} 
     124 
     125=== Error reporting === 
     126 
     127 * For engine bugs (that is, error cases which need to be fixed by C++ developers, and which should never be triggered by users or modders (even if they write invalid data files)), use "`debug_warn(L"message")`" to report the error. (This pops up an ugly dialog box with stack trace and continue/debug/exit buttons.) 
     128 
     129 * For error cases that could be triggered by modders (e.g. invalid data files), use 
     130{{{ 
     131LOGERROR(L"Failed to load item %d from file %ls", i, path.c_str()); 
     132}}} 
     133 This gets display on screen in red, and saved in the log files. 
     134 ''Exception:'' Code in `source/lib/` can't use `LOGERROR` (it can only use things defined in `source/lib/`). 
     135 
     136 * The engine should try to cope gracefully with `LOGERROR` cases, e.g. abort loading the current file; it should never crash in those cases. 
     137 
    98138=== Documentation === 
    99139 
     
    125165=== Strings === 
    126166 
    127  * Use `CStr` instead of `std::string`. Use `CStrW` instead of `std::wstring`. (These are subclasses of `std::[w]string` with various extra methods added for conveniene.) 
     167 * Use `CStr` instead of `std::string`. Use `CStrW` instead of `std::wstring`. (These are subclasses of `std::[w]string` with various extra methods added for convenience.) 
     168  * ''Exception:'' `source/lib/` and `source/simulation2/` and `source/scriptinterface/` tend to prefer `std::[w]string` instead. 
    128169 
    129170 * For portability, use the following formats for printf-style functions: 
     
    139180 * In header files, avoid `#include` and use forward declarations wherever possible. 
    140181 
     182 * Sometimes it's nice to put `#include`s in alphabetical order. 
     183 
    141184 * 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). 
    142185 
     
    149192  * When unavoidable, global variables should be named with a `g_` prefix. 
    150193  * Prefer global variables over singletons, because then they're not trying to hide their ugliness. 
     194 
     195 * Don't do "`if (p) delete p;`". (That's redundant since "`delete NULL;`" is safe and does nothing.) 
     196 
     197 * If deleting a pointer, and it's not in a destructor, and it's not being immediately assigned a new value, use "`SAFE_DELETE(p)`" (which is equivalent to "`delete p; p = NULL;`") to avoid dangling pointers to deleted memory. 
     198 
     199== !JavaScript == 
     200 
     201 * Use the same basic formatting as described above for C++. 
     202 
     203 * Use roughly the same Doxygen-style comments for documentation as described above for C++. (But we don't actually run Doxygen on the JS code, so there's no need to make the comments use technically correct Doxygen syntax.) 
     204 
     205 * Don't omit the optional semicolons after statements. 
     206 
     207 * Use quotes around the key names in object literals: 
     208{{{ 
     209var x = 100, y = 200; 
     210var pos = { "x": x, "y": y }; 
     211}}} 
     212 
     213 * Create empty arrays and objects with "`[]`" and "`{}`" respectively, not with "`new Array()`" and "`new Object()`". 
     214 
     215 * Non-standard !SpiderMonkey extensions to the JS language may be used (though prefer to use equivalent standard features when possible). Documentation: [https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.6 1.6], [https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.7 1.7], [https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.8 1.8], [https://developer.mozilla.org/En/JavaScript/New_in_JavaScript/1.8.1 1.8.1], [https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.8.5 1.8.5], [https://developer.mozilla.org/en/JavaScript_typed_arrays typed arrays]. 
     216 
     217 * To convert a string to a number, use the "`+`" prefix operator (not e.g. `parseInt`/`parseFloat`): 
     218{{{ 
     219var a = "1"; 
     220var b = a + 1;     // string concatenation; b == "11" 
     221var c = (+a) + 1;  // numeric addition; c == 2 
     222}}}