Skip to content

Commit 1e16d62

Browse files
committed
doc: Clarify namespace usage rules
1 parent ae0d3ec commit 1e16d62

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

doc/contributing/source/coding-style.rst

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,10 +1213,46 @@ the issue. In the following example, ``x`` can be declared as ``float`` instead
12131213
Namespaces
12141214
==========
12151215

1216-
Code should always be included in a given namespace, namely ``ns3``.
1217-
In order to avoid exposing internal symbols, consider placing the code in an
1218-
anonymous namespace, which can only be accessed by functions in the same file.
1219-
1216+
|ns3| uses the ``ns3`` namespace to separate ns-3 model library code from the
1217+
C++ global namespace. The following guidelines apply to the use of
1218+
the ``ns3`` namespace and additional namespaces. Note that these guidelines are
1219+
applied inconsistently within the |ns3| mainline because |ns3| historically had
1220+
limited use of nested namespaces, and a widespread change to use nested namespaces
1221+
in all libraries would hinder backward compatibility of user programs. Therefore,
1222+
the migration to use the below guidelines is gradual, but they should apply to
1223+
newly authored code.
1224+
1225+
- |ns3| model library code (code within the ``model`` and ``helper`` directories)
1226+
should be included within the ``ns3`` namespace.
1227+
- We recommend that new ns-3 modules, intended for the mainline, wrap their
1228+
model code in a nested namespace. The name of the namespace should be the
1229+
module name in lowercase, without any hyphens. See the ``lr-wpan`` module
1230+
for an example (the nested namespace in that case is ``lrwpan``). An example
1231+
of a recently added module following this guideline is the ``zigbee`` module.
1232+
- Tests should use the ``tests`` namespace outside of the ``ns3`` namespace,
1233+
and can import the ``ns3`` namespace and any nested namespaces with the ``using``
1234+
directive.
1235+
- Example program code should not be within the ``ns3`` or nested namespaces, but
1236+
can import those namespaces with the ``using`` directive.
1237+
- In order to avoid exposing internal symbols, consider placing such code in an
1238+
anonymous namespace, which can only be accessed by functions in the same file.
1239+
1240+
When using nested namespaces, a question arises as to the use of prefixes on types
1241+
that are declared in the namespace. For instance, consider the ``LrWpanNetDevice``
1242+
class, which is a derived class of ``NetDevice`` and which exists in the
1243+
``ns3::lrwpan`` namespace. When using scope resolution to refer to these types,
1244+
the prefix is repetitive with the namespace; e.g., ``ns3::lrwpan::LrWpanNetDevice``,
1245+
although ``ns3::lrwpan::NetDevice`` would be sufficient to avoid collisions and
1246+
would be shorter. However, use of the shorter version can hinder regular expression
1247+
searches through the codebase for given types, and require more instances of scope
1248+
resolution. This style
1249+
guide leaves this decision of whether to prefix type names with nested namespace
1250+
hints (such as ``LrWpanNetDevice``) or to shorten the type names (such as
1251+
``NetDevice``) for authors to decide, but in the case where a name may be the same
1252+
as used in another module (such as the NetDevice example herein), a prefix on the
1253+
type name is recommended.
1254+
1255+
The following guidelines apply to the code syntax for namespace usage.
12201256
Code within namespaces should not be indented. To more easily identify the end
12211257
of a namespace, add a trailing comment to its closing brace.
12221258

@@ -1229,8 +1265,6 @@ of a namespace, add a trailing comment to its closing brace.
12291265

12301266
} // namespace ns3
12311267

1232-
Namespace names should follow the snake_case convention.
1233-
12341268
Unused variables
12351269
================
12361270

0 commit comments

Comments
 (0)