You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found a lot of instances of using namespace xyz; in the repo. This is bad practice because it pollutes the global namespace, and negates the whole benefit of namespaces in the first place, which is to avoid collisions. It seems that the pollution is isolated only to .cpp files which is not as bad, but still should be avoided.
So what to do:
When its in a cpp file for say a driver(ex. aeat.cpp or similar), then just have the .cpp file have an outer namespace:
namespace foo {
namespace bar {
namespace baz {
int qux = 42;
}
}
}
namespace fbz = foo::bar::baz; // This is namespace aliasing. Basically just a short form.
int main()
{
std::cout << fbz::qux << '\n';
}
If you're using something over and over again from a different namespace, you can technically do smth like this:
#include <ostream>
using ::std::cout;
int main(int argc, char** argv)
{
int rc = do_some_stuff(argc, argv);
using ::std::endl;
if (rc) { // print the success report
using ::std::cout;
cout << "The test run completed. The return code was " << rc << '.' << endl;
} else {
using ::std::cerr;
cerr << "Unable to complete the test run." << endl;
}
return 0 == rc;
}
A part of this ticket may be reorganizing some of the folders/files under libs where there may be some excessive namespacing (mostly my fault oops). one example is PID::PID::PID. could probs put the PID class in a more general namespace instead of a PID namespace. Control? Alogorithm? idk yet. Also the namespacing practices above would avoid some of the PID::PID::PID issues all together.
I found a lot of instances of
using namespace xyz;
in the repo. This is bad practice because it pollutes the global namespace, and negates the whole benefit of namespaces in the first place, which is to avoid collisions. It seems that the pollution is isolated only to .cpp files which is not as bad, but still should be avoided.So what to do:
When its in a cpp file for say a driver(ex. aeat.cpp or similar), then just have the .cpp file have an outer namespace:
If its a namespace for something thats included for elsewhere(ex. from a file thats included), AND the name is really really long, you can use a namespace alias(https://en.cppreference.com/w/cpp/language/namespace_alias):
If you're using something over and over again from a different namespace, you can technically do smth like this:
BUT I think we should still avoid this.
I think here is a good instance where we should just follow the google style guide where the only permitted use of
using
is for type aliasing (https://en.cppreference.com/w/cpp/language/type_alias)A part of this ticket may be reorganizing some of the folders/files under libs where there may be some excessive namespacing (mostly my fault oops). one example is PID::PID::PID. could probs put the PID class in a more general namespace instead of a PID namespace. Control? Alogorithm? idk yet. Also the namespacing practices above would avoid some of the PID::PID::PID issues all together.
This is a good read:
https://softwareengineering.stackexchange.com/questions/236404/what-is-using-namespace-pollution/236424
The text was updated successfully, but these errors were encountered: