Is it worth it to have separate namespaces for interfaces and implementations?
Stroustrup's advice in his C++ book (fourth edition) is that we should use separate namespaces for interfaces and implementations. Can the more experienced folks say something on this one? I mean it sounds nice, but is it really practical, does it make sense in real world projects?
Defines an interface in a separate package from its implementation. As you develop a system, you can improve the quality of its design by reducing the coupling between the system's parts. A good way to do this is to group the classes into packages and control the dependencies between them.
Because interfaces can be implemented by multiple components, it's good practice to put them in a separate assembly from that of the implementing components.
A namespace tells you something about who the definition belongs to. Of course it makes sense for the interface to belong to a different group than the implementation; that's the whole point of interfaces, separation of concerns.
In code that is heavy on implementation (say, some meta-programming monstrosity inside Boost), it can be useful to spot at a glance which code you are expected to be able to use directly, and which code you can safely ignore. Code in a library's detail
namespace is deemed to be "internal" code, so you don't need to spend time hunting through documentation when you see a detail
symbol in a stack trace.
I wouldn't say there's a huge benefit to it and certainly not so in the general case, but since it doesn't do any harm you might as well keep things tidy and segregated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With