Long nested namespace and source code readability & complexity #39
Replies: 12 comments
-
Personally I am not disturbed by expliciting namespaces, at least the code is a bit easier to follow. 'using namespace' is somehow hiding information. Also remember that you can add typedef inside your class. Maybe is it possible to do trickier things with c++11???! |
Beta Was this translation helpful? Give feedback.
-
Or replace typedef keyword by using keyword for the type as c++11 is allowed: |
Beta Was this translation helpful? Give feedback.
-
I all, On my side I think that the 'using' directive (the classical 'using namespace': http://en.cppreference.com/w/cpp/language/namespace) is really bad and is hiding information (It is in fact similar to the python "from xx import *"), for this reason is should be forbidden into the .h and .inl. But it also seems to me that the situation of the 'using' declaration (http://en.cppreference.com/w/cpp/language/namespace) as well as the 'using' type alias (http://en.cppreference.com/w/cpp/language/type_alias) is really different. As they are 'explicit' they do not hide anything and we can control their visibility as python developpers are doing with the "from xx import MyVec as Vec3". So given the fact that the latter two are:
DM. |
Beta Was this translation helpful? Give feedback.
-
Many years ago during a technical meeting we made two decisions that were never fully implemented :
But unfortunately it was low priority and not put into work... yet ;) |
Beta Was this translation helpful? Give feedback.
-
Maybe doing namespace sofa
{
namespace core
{
using sofa::core::verydeeptnamespace::oldnamingscheem::AClass ;
}
} Would allow to expose the old namespace prefix to the new scheme without the need to change the existing code base. This could help during a transitional period because the existing code wouldn't need to be changed at a single time. DM. |
Beta Was this translation helpful? Give feedback.
-
That's a good idea! The same could be done for the include path, temporarily keeping Regarding the earlier point about using
But now with c++11, if
A good improvement :) |
Beta Was this translation helpful? Give feedback.
-
I tried the private namespace trick + 'using' to support backward compatibility in Feedback welcome. |
Beta Was this translation helpful? Give feedback.
-
Hi all I would be very happy if at the STC we re-warm the decision about having a clear naming scheme. My current position on the issue is that:
To me there is three interesting feature namespace should have each grounded by different needs: Since c++11 it is very possible to have all that with a dual namespace scheme in which one namespace match the files location. But as this namespace is in general long we have a second namespace organization that export the names in a more accessible namespace. Ex (for a class in sofa/somehwere/again/MyClass.h): /// namespace (hierarchical grouping)
namespace sofa {
namespace somewhere {
namespace again {
namespace __myclass__ {
/// This is a private namespace that I like a lot :)
/// because we can import things we want with name leacking all around
/// No more typedef when they are not needed :)
/// The name here are available in __myclass__ (so normally only
/// in the .cpp and .h and .inl)
using std::vector;
using sofa::defaultype::Vec;
using sofa::suplercomplex::longns::OtherClass ;
class MyClass : OtherClass
{
MyCLass(Vec<3,float>&, ...) ;
};
}
}
}
} Then come the logical grouping in which the MyClass is exported into its logical group with 'using' /// The second namespace schema (logical grouping)
namespace sofa {
namespace component {
namespace collision {
using sofa::somewhere::again::__myclass__::MyClass ;
}
}
} So generally I such approach that this increase a lot the readability of the source code and conceptually this gives a taste of how python is handling import of modules and avoid nameclash. |
Beta Was this translation helpful? Give feedback.
-
@fredroy it could be nice to define the new policy in the guidelines, in this discussion and subsequently close it |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
According to the SOFA guidelines, we have the following rule:
"You must avoid the using directive in header files (.h and .inl): using namespace foo."
The reason for this rule is that doing so generates namespace pollution which increase the risk of name collision. The drawback is that the long nested namespaces prefixes are repeated all around in the .h and .inl which impact readability.
What do you think of changing the rules and allowing to use "using namespace" and 'using' in the following context:
With such design the names imported in the boxroi namespace by 'using' and 'using namespace' are not leacking in the engine/component/sofa namespace.
I see no side-effect of such design but maybe you do.
DM.
Beta Was this translation helpful? Give feedback.
All reactions