Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure Libraries and Namespaces #347

Open
wmmc88 opened this issue Apr 10, 2021 · 1 comment
Open

Restructure Libraries and Namespaces #347

wmmc88 opened this issue Apr 10, 2021 · 1 comment
Assignees

Comments

@wmmc88
Copy link
Member

wmmc88 commented Apr 10, 2021

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:

namepace encoder {
//code here
}

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):

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;
}

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

@wmmc88 wmmc88 self-assigned this Apr 10, 2021
@wmmc88
Copy link
Member Author

wmmc88 commented Apr 10, 2021

And part of this should also be cleaning up some of the public/private/interface target_link_libraries options.

Only included in the header: public
Only included in the cpp: private
header only target: interface

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant