| 98 | * Write switch statements like |
| 99 | {{{ |
| 100 | switch (n) |
| 101 | { |
| 102 | case 10: |
| 103 | return 1; |
| 104 | |
| 105 | case 20: |
| 106 | foo(); |
| 107 | // fall through to next case [this should be explicit if you don't end with break or return] |
| 108 | |
| 109 | case 30: |
| 110 | bar(); |
| 111 | break; |
| 112 | |
| 113 | case 40: |
| 114 | { |
| 115 | int i = n*2; // [need the extra {...} scope when declaring variables inside the case] |
| 116 | return i; |
| 117 | } |
| 118 | |
| 119 | default: |
| 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 | {{{ |
| 131 | LOGERROR(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 | |
| 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 | {{{ |
| 209 | var x = 100, y = 200; |
| 210 | var 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 | {{{ |
| 219 | var a = "1"; |
| 220 | var b = a + 1; // string concatenation; b == "11" |
| 221 | var c = (+a) + 1; // numeric addition; c == 2 |
| 222 | }}} |