Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-88322

Enforce ban on "using directives", especially in headers

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Server Programmability


      I'm seeing a lot of "using namespace" in header files at namespace scope. A lot. This is a banned practice.

      https://google.github.io/styleguide/cppguide.html#Namespaces

      https://abseil.io/tips/153

      "using namespace" should basically only be used to bring in UDLs or other namespaces with carefully curated minimal populations.

      I think it's very important that we hold the line on this, as these directives, always iffy, become "viral" when they're in headers.

      The ones at global scope are particularly dangerous.
      We have a rule that almost nothing at all should be touching the global namespace!

      I have had to sweep the codebase to remove using namespace fmt::literals; from headers in the recent past. There are now 2 more appearances of just that one.

      A few hits that turn up with a primitive grep (in no order):
      planner.h: (global scope)
      using namespace mongo::literals;
      encryption_schema_tree.h : (global scope)
      using namespace mongo::query_analysis;
      src/mongo/db/modules/enterprise/src/streams/exec/test_constants.h: (global scope)
      using namespace mongo::literals;
      src/mongo/db/query/datetime/date_time_support.h: (in namespace mongo).
      using namespace std::string_literals;
      src/mongo/client/server_discovery_monitor.h: (in namespace mongo)
      using namespace sdam;

      People are perhaps operating on the idea that a header's declarations are somehow encapsulated or safer than they really are.
      I'm not really sure. But it's really incorrect C++ and we should fix it all up and think of a way to stop people from doing it.

            Assignee:
            Unassigned Unassigned
            Reporter:
            billy.donahue@mongodb.com Billy Donahue
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: