On 17.11.2017 at 12:48 PM, Finalspace said:
I am talking about nested namespaces in C++. For example:
Note that in C++17, there is a feature actually called "nested namespaces", which can make such code way easier to read/write:
// might be worth a discussion if this is actually better when there are multiple nested namespaces in one file
namespace my::paths {
const char *ExtractFileName(const char *path);
namespace my::strings {
size_t GetAnsiStringLength(const char *str);
};
// could instead look like this:
namespace my {
namespace system::io::path {
const char *ExtractFileName(const char *path);
};
namespace strings {
size_t GetAnsiLength(const char *str);
};
};
On 17.11.2017 at 8:18 AM, Finalspace said:
I personally think that C++ namespaces are there to prevent conflicts between other api´s and for nothing else. But for medium to large size api´s one nested level is fine.
They are also a better alternative for "smurf naming convention". I worked at a project where every class was prefixed with proj_module_nested_ ... you could argue that this is already an iditiotic naming scheme, but in multi-million line projects, its important to be able to quickly see to which module a certain method/class belongs.
On 17.11.2017 at 8:18 AM, Finalspace said:
Too long: my::IO::Path::Combine(my::IO::Path::GetHome(), my::IO::Path::ExtractFileName(...));
Fine: my::Paths::CombinePath(my::Paths::GetHomePath(), my::Paths::ExtractFileName(...));
Better (using namespace my): Paths::CombinePath(Paths::GetHomePath(), Paths::ExtractFileName(...));
I'd actually prefer IO::Paths::GetHome() etc... I tend to group my global methods in a static class instead of a namespace, so I'm just used to seeing multiple levels of nested :: without a problem. I also tend to not using namespace, but instead put all implementations in their respective namespace as well:
namespace my::strings
{
const char* something(const char* path)
{
IO::Paths::GetHomePath(); // my is implicitely available, as well as my::strings.
}
}
But thats just my sense, in case you want to get some different ideas.