Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it worth it to have separate namespaces for interfaces and implementations?

Tags:

c++

c++11

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?

like image 766
user3111311 Avatar asked Jan 08 '14 13:01

user3111311


People also ask

Why do we need to separating interface from implementation?

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.

Should interfaces be in separate project?

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.


2 Answers

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.

like image 107
Mark Ransom Avatar answered Sep 26 '22 02:09

Mark Ransom


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.

like image 20
Lightness Races in Orbit Avatar answered Sep 22 '22 02:09

Lightness Races in Orbit