Changes between Version 43 and Version 44 of MapGuideCodingStandards


Ignore:
Timestamp:
09/19/22 05:56:31 (2 years ago)
Author:
jng
Comment:

More localization and C++11 notes

Legend:

Unmodified
Added
Removed
Modified
  • MapGuideCodingStandards

    v43 v44  
    66
    77||'''Revision'''||'''Date'''||'''Author'''||'''Comment'''||
    8 ||1.6.4||19 Sep, 2022||Jackie Ng||Clarified expected C++ standards level and added some considerations on public API naming/design||
     8||1.6.4||19 Sep, 2022||Jackie Ng||Clarified expected C++ standards level, added some considerations on public API naming/design and more notes||
    99||1.6.3||26 Jan, 2015||Jackie Ng||Added section about argument checking||
    1010||1.6.2||9 Dec, 2012||Jackie Ng||More about C++ documentation||
     
    687687}
    688688}}}
     689
     690=== C++11 language constructs ===
     691
     692As C++11 is the base language standard that the MapGuide/FDO codebase is expected to be compiled in, you are actively encouraged to take advantage of any language features/constructs introduced with this particular version of C++.
     693
     694As MapGuide/FDO was originally conceived before the introduction of C++11, there will be vast places in the codebase where code is written in the pre-C++11 style. You are encouraged (but not required) to migrate such existing code to the C++11 style if you encounter/touch it in your journey of fixing a bug or implementing a new feature. 2 key constructs C++11 constructs worthy of special consideration are detailed below
     695
     696==== `auto` ====
     697
     698Use `auto` where possible to let the C++ compiler infer the type of a local variable based on the type of the right-hand side of a variable assignment. This is most useful when dealing with STL iterators or sufficiently complex templated type where you can avoid having to specify the type like this:
     699
     700{{{
     701std::vector<int>::iterator it = some_int_vector.begin();
     702}}}
     703
     704and just declare it like this:
     705
     706{{{
     707auto it = some_int_vector.begin();
     708}}}
     709
     710`auto` also has useful applications for primitive variable declarations as it can fix up to one half of any potential `data type mismatch` warnings as a result of declaring the variable with an incorrect (or insufficiently wide) type on the left-hand side of a variable assignment (think `size_t`/`int` indexing mismatches in for loops for an example)
     711
     712However, do not use `auto` as the return type (aka. return type deduction) in any public API method signature. We need to be as verbose and concise as possible for SWIG to generate the correct proxy classes.
     713
     714==== for loops ====
     715
     716When working with STL iterators it is much more preferred to use the simplified range-based for loops introduced in C++11
     717
     718Pre C++11 loops look like this:
     719
     720{{{
     721for (some_type::iterator it = container.begin(); it != container.end(); it++)
     722{
     723   some_item_type item = *it;
     724   ...
     725}
     726}}}
     727
     728Such loops can be simplified to this:
     729
     730{{{
     731for (auto const & item : container)
     732{
     733    ...
     734}
     735}}}
     736
     737With adjustments to whether the item needs to be `const` or by reference depending on the mutability needs of the iterated item.
    689738
    690739== Pointers ==
     
    9581007=== C++ ===
    9591008
    960 All strings which need to be localized must be placed into res resource files.
     1009All strings which need to be localized must be placed into res resource files. The main string bundle resides in `Common/MapGuideCommon/Resources/mapguide_en.res`
    9611010
    9621011=== General ===
    9631012
    964 Strings which do not need to be localized can remain in the code.  However, you should indicate the string is non-localizable by adding a NOXLATE comment, e.g.
     1013Strings which do not need to be localized can remain in the code.  However, you should indicate the string is non-localizable by adding a `NOXLATE` comment, e.g.
    9651014{{{
    9661015#!cpp
     
    9711020}}}
    9721021
    973 The comment indicates that the author of the code explicitly wants to exclude the string from being localized.
     1022The comment indicates that the author of the code explicitly wants to exclude the string from being localized and also facilitates detection of un-localized strings. Any matches for string literal searches without the instance of the `NOXLATE` comment on that line is a strong hint that such a string is meant to be localized.
    9741023
    9751024Exception string messages should be globalized if possible.
    9761025
     1026Debugging/debug-only messages do not require localization and is assumed to be in English.
     1027
    9771028== Special Comments ==
    9781029