Uploaded image for project: 'Data Management'
  1. Data Management
  2. DM-1510

C++ Standards Rule 3-9 needs rewrite

    XMLWordPrintable

    Details

    • Story Points:
      1
    • Team:
      Architecture

      Description

      Jim Bosch reviewed Section 3 of the C++ Standard. He noted this change which I felt warranted SAT clarification:

      3-9: This is a confusing conflation of two entirely different concepts: it seems to discourage all global variables, regardless of whether they're in a namespace, but only to discourage free functions when they aren't in namespace. If that reading is correct, I think both are sensible recommendations, but they need to be clarified, and probably split up. If the reading should be that all free functions are discouraged, that'd be a terrible rule we violate all over the place. If the reading should be that global variables are only discouraged when not in a namespace, that needs to be clarified (and IMO it could be bumped up to a complete prohibition).

      Refer to: https://confluence.lsstcorp.org/pages/viewpage.action?pageId=16908685 Rule 3.9. The current Rule states:

      3-9. Global variables and functions SHOULD be avoided and if used MUST always be referred to using the '::' operator.

      ::mainWindow.open(), ::applicationContext.getName(), ::erf(1.0)
      

      In general, the use of global variables should be avoided. Consider using singleton objects instead. Only use where required (i.e. reusing a framework that requires it.) See Rule 5-7 ( .https://confluence.lsstcorp.org/pages/viewpage.action?pageId=16908706#C++Statements-5-7 ).

      Global functions in the root namespace that are defined by standard libraries can often be avoided by using the C++ versions of the include files (e.g. #include <cmath> instead of #include <math.h>). Since the C++ include files place functions in the std namespace, using namespace std;, which is permitted by Rule 5-41, will allow these functions to be called without using the :: operator. In cases where functions are only available in the C include files, the :: operator must be used to call them. This requirement is intended to highlight that these functions are in the root namespace and are different from class methods or other namespaced free functions.

        Attachments

          Issue Links

            Activity

            Hide
            robyn Robyn Allsman [X] (Inactive) added a comment -

            Jim Bosch, would you please take this on. Write up an RFC with the changes you propose. Should this be accepted then someone (you or whatever TCT shadow approves it since this is a Standards change) needs to update the Confluence reference copy and also the Docushare version if keeping that up-to-date that is still relevant..

            Show
            robyn Robyn Allsman [X] (Inactive) added a comment - Jim Bosch , would you please take this on. Write up an RFC with the changes you propose. Should this be accepted then someone (you or whatever TCT shadow approves it since this is a Standards change) needs to update the Confluence reference copy and also the Docushare version if keeping that up-to-date that is still relevant..
            Hide
            swinbank John Swinbank added a comment -

            Since the Project Engineer owns the coding standards, I'm assigning this to him. If Jim Bosch wants to take this further, he should RFC it.

            Show
            swinbank John Swinbank added a comment - Since the Project Engineer owns the coding standards, I'm assigning this to him. If Jim Bosch wants to take this further, he should RFC it.
            Hide
            gpdf Gregory Dubois-Felsmann added a comment - - edited

            To the extent there is any ambiguity about the policy on global functions:

            It is very much part of the spirit of C++ for there to be global functions associated with namespaces. The name resolution mechanism anticipates this. Often, but not necessarily, these would be friend functions. I wouldn't want class designs that include associated namespaced global functions to be banned. However, from a design standpoint, it is desirable if those functions are relatively "thin" and provide axes of extensibility, either via polymorphism (through having their substantive work carried out by class member functions) or templating.

            Show
            gpdf Gregory Dubois-Felsmann added a comment - - edited To the extent there is any ambiguity about the policy on global functions: It is very much part of the spirit of C++ for there to be global functions associated with namespaces. The name resolution mechanism anticipates this. Often, but not necessarily, these would be friend functions. I wouldn't want class designs that include associated namespaced global functions to be banned. However, from a design standpoint, it is desirable if those functions are relatively "thin" and provide axes of extensibility, either via polymorphism (through having their substantive work carried out by class member functions) or templating.
            Hide
            tjenness Tim Jenness added a comment -

            Where are we with this rewrite?

            Show
            tjenness Tim Jenness added a comment - Where are we with this rewrite?
            Hide
            tjenness Tim Jenness added a comment -

            The current location of this rule is at https://developer.lsst.io/coding/cpp_style_guide.html#global-variables-and-functions-should-be-avoided-and-if-used-must-always-be-referred-to-using-the-operator

            It looks like the current version matches that described in this ticket.

            Show
            tjenness Tim Jenness added a comment - The current location of this rule is at https://developer.lsst.io/coding/cpp_style_guide.html#global-variables-and-functions-should-be-avoided-and-if-used-must-always-be-referred-to-using-the-operator It looks like the current version matches that described in this ticket.
            Hide
            gpdf Gregory Dubois-Felsmann added a comment -

            "Matches" in the sense of "still in the state originally complained about in the ticket", yes.

            Show
            gpdf Gregory Dubois-Felsmann added a comment - "Matches" in the sense of "still in the state originally complained about in the ticket", yes.

              People

              Assignee:
              ktl Kian-Tat Lim
              Reporter:
              robyn Robyn Allsman [X] (Inactive)
              Watchers:
              Frossie Economou, Gregory Dubois-Felsmann, Jim Bosch, John Swinbank, Jonathan Sick, Kian-Tat Lim, Krzysztof Findeisen, Tim Jenness
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

                Dates

                Created:
                Updated:

                  Jenkins

                  No builds found.