Changes between Version 30 and Version 31 of Coding_Conventions
- Timestamp:
- Jan 10, 2017, 1:34:41 AM (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Coding_Conventions
v30 v31 17 17 {{{ 18 18 #!cpp 19 /* Copyright (C) 201 6Wildfire Games.19 /* Copyright (C) 2017 Wildfire Games. 20 20 * This file is part of 0 A.D. 21 21 * … … 39 39 {{{ 40 40 #!cpp 41 /* Copyright (c) 201 6Wildfire Games41 /* Copyright (c) 2017 Wildfire Games 42 42 * 43 43 * Permission is hereby granted, free of charge, to any person obtaining … … 123 123 } 124 124 125 case 50: 126 { 127 int z = n*2; 128 n = foobar(n, z); 129 break; // [place breaks inside the brackets] 130 } 131 125 132 default: 126 133 debug_warn(L"invalid value for n"); // [only do this kind of warning if this case is an engine bug] … … 236 243 std::vector<T> anyVector; 237 244 std::for_each(anyVector.begin(), anyVector.end(), [] (const T& element){ 238 // code245 // code 239 246 } 240 247 … … 242 249 for (const T& element : anyVector) 243 250 { 244 // code251 // code 245 252 } 246 253 }}} … … 256 263 { 257 264 return param + optionalParam; 265 } 266 }}} 267 268 * In `c++11`, it is possible to use the `auto` type-specifier, wherein the appropriate data-type is determined at compile time. Although potentially useful, overuse can cause serious problems in the long-run. Therefore, this code feature should be used in moderation. Specifically: 269 - It should be clear from reading the code what type the `auto` is replacing. (Add a comment if necessary.) 270 - It should only ever be used as a replacement for an iterator type. 271 - It should only be used if the data-type-specifier it is standing in for is long. As a rule of thumb, if the line would be shorter than your chosen suitable line width (see convention on avoiding wide lines under [#Formatting formatting] above) without it, don't use it. 272 273 * Favour early returns where possible. The following: 274 {{{ 275 #!cpp 276 void foo(bool x) 277 { 278 if (x) 279 { 280 /* lines */ 281 } 282 } 283 }}} 284 is better when written like: 285 {{{ 286 #!cpp 287 void foo(bool x) 288 { 289 if (!x) 290 return; 291 292 /* lines */ 258 293 } 259 294 }}} … … 269 304 {{{ 270 305 #!js 271 varx = 100, y = 200;272 varpos = { "x": x, "y": y };306 let x = 100, y = 200; 307 let pos = { "x": x, "y": y }; 273 308 }}} 274 309 275 310 * Create empty arrays and objects with "`[]`" and "`{}`" respectively, not with "`new Array()`" and "`new Object()`". 276 311 312 * Global variables and constants are named with a `g_` prefix and the rest of the name in !CamelCase, ie. `var g_ParsedData` or `const g_BarterActions`. 313 277 314 * 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]. 278 315 … … 280 317 {{{ 281 318 #!js 282 vara = "1";283 varb = a + 1; // string concatenation; b == "11"284 varc = (+a) + 1; // numeric addition; c == 2319 let a = "1"; 320 let b = a + 1; // string concatenation; b == "11" 321 let c = (+a) + 1; // numeric addition; c == 2 285 322 }}} 286 323 … … 304 341 }}} 305 342 343 * Use `var` only when declaring variables in the global scope. When inside functions, ifs, loops, etc., use `let`. We prefer `let` because: 344 - It is restricted to the scope it is declared in, preventing unintentional clobbering of values 345 - It is stricter than `var`, and throws an error if it a variable is declared twice within the same scope 346 {{{ 347 #!js 348 { 349 var x = 1; 350 var x = "foo" // No Error 351 } 352 353 { 354 let x = 1; 355 let x = "foo"; // TypeError: redeclaration of let x 356 } 357 }}} 358 306 359 == XML == 307 360 * All XML files should start with