Changes between Version 30 and Version 31 of Coding_Conventions


Ignore:
Timestamp:
Jan 10, 2017, 1:34:41 AM (7 years ago)
Author:
s0600204
Comment:

Bump to 2017, and have a go at adding conventions adopted over the past year (or two).

Legend:

Unmodified
Added
Removed
Modified
  • Coding_Conventions

    v30 v31  
    1717{{{
    1818#!cpp
    19 /* Copyright (C) 2016 Wildfire Games.
     19/* Copyright (C) 2017 Wildfire Games.
    2020 * This file is part of 0 A.D.
    2121 *
     
    3939{{{
    4040#!cpp
    41 /* Copyright (c) 2016 Wildfire Games
     41/* Copyright (c) 2017 Wildfire Games
    4242 *
    4343 * Permission is hereby granted, free of charge, to any person obtaining
     
    123123}
    124124
     125case 50:
     126{
     127    int z = n*2;
     128    n = foobar(n, z);
     129    break;             // [place breaks inside the brackets]
     130}
     131
    125132default:
    126133    debug_warn(L"invalid value for n");   // [only do this kind of warning if this case is an engine bug]
     
    236243std::vector<T> anyVector;
    237244std::for_each(anyVector.begin(), anyVector.end(), [] (const T& element){
    238    //code
     245   // code
    239246}
    240247
     
    242249for (const T& element : anyVector)
    243250{
    244    //code
     251   // code
    245252}
    246253}}}
     
    256263{
    257264   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
     276void foo(bool x)
     277{
     278    if (x)
     279    {
     280        /* lines */
     281    }
     282}
     283}}}
     284   is better when written like:
     285{{{
     286#!cpp
     287void foo(bool x)
     288{
     289    if (!x)
     290        return;
     291
     292    /* lines */
    258293}
    259294}}}
     
    269304{{{
    270305#!js
    271 var x = 100, y = 200;
    272 var pos = { "x": x, "y": y };
     306let x = 100, y = 200;
     307let pos = { "x": x, "y": y };
    273308}}}
    274309
    275310 * Create empty arrays and objects with "`[]`" and "`{}`" respectively, not with "`new Array()`" and "`new Object()`".
    276311
     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
    277314 * 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].
    278315
     
    280317{{{
    281318#!js
    282 var a = "1";
    283 var b = a + 1;     // string concatenation; b == "11"
    284 var c = (+a) + 1;  // numeric addition; c == 2
     319let a = "1";
     320let b = a + 1;     // string concatenation; b == "11"
     321let c = (+a) + 1;  // numeric addition; c == 2
    285322}}}
    286323
     
    304341}}}
    305342
     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
    306359== XML ==
    307360 * All XML files should start with