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

Use C++ namespaces instead of "nd" prefix #229

Open
jonesmz opened this issue Feb 27, 2021 · 8 comments
Open

Use C++ namespaces instead of "nd" prefix #229

jonesmz opened this issue Feb 27, 2021 · 8 comments

Comments

@jonesmz
Copy link
Contributor

jonesmz commented Feb 27, 2021

Why are functions and classes, such as https://github.com/MADEAPPS/newton-dynamics/blob/master/newton-4.00/sdk/dNewton/ndBodyParticleSet.h being prefixed with "nd". That's what namespaces are for?

@JulioJerez
Copy link
Contributor

yes, but name spaces are a suggestive thing. some people like them and soem other people prefer the prefix, I am in teh later group.

@jonesmz
Copy link
Contributor Author

jonesmz commented Mar 1, 2021

You are polluting the global namespace with your symbols. This is strongly undesired by a lot of projects that might want to use Newton.

If you put everything into a namespace, any project that wants to use your code can pull your symbols into their preferred namespace, or the global namespace, with "using namespace newton".

But projects that don't want prefixes like "nd" cant do anything about it. They are forced to use your prefix.

If you want to have "nd" in front of everything for your code, consider making a macro. E.g. #define NEWTON_PREFIX(symbol) nd##symbol so that consumers of your code can specify their own prefix if needed.

@iSLC
Copy link
Contributor

iSLC commented May 11, 2021

A lot of other projects do this. Bullet does it with bt, Box2d does it with b2 and so on. At this point I'd rather say that its because it allows a somewhat C-like interface when built as a dynamic library/shared object. Symbol names are cleaner when exported. A namespace may introduce name mangling which may differ from compiler to compiler. If he wants to do it this way, then let it be. It has purpose even if we don't see it beyond our current needs.

I suppose one could argue that a namespace may be added as a conditional macro. But is fine as it is so far.

@jonesmz
Copy link
Contributor Author

jonesmz commented May 11, 2021

Name mangling happens with C++ code, regardless of the use of namespaces.

If you want a C interface, make an actual C interface.

@Hedede
Copy link

Hedede commented Mar 28, 2022

I haven't actually seen a physics engine that doesn't do this. Havok uses the hk prefix, Bullet uses bt, PhysX uses Np etc

@jonesmz
Copy link
Contributor Author

jonesmz commented Mar 28, 2022

And everyone else jumping off of a bridge means we should as well?

It's trivial to macro-ize this to allow for the consumer of the library to use a prefix or a namespace, at their choice.

#ifdef USE_CPP_NAMESPACE
#define NEWTON_NAMESPACE(symbol) nd::#symbol
#else
#define NEWTON_NAMESPACE(symbol) nd#symbol
#endif

But inflecting global symbols on the consumer of the library is not polite.

@jonesmz jonesmz closed this as completed Mar 28, 2022
@jonesmz jonesmz reopened this Mar 28, 2022
@Hedede
Copy link

Hedede commented Mar 29, 2022

I don't see it as a big deal if everything is correctly prefixed. It doesn't really "pollute" anything. Name conflicts are unlikely, and it won't show up in the code completion unless you type the prefix.

I think it is better to keep prefixes than to "macro-ize" everything. Namespaces are of course better IMO, but it is better to spend the effort elsewhere.

@JulioJerez
Copy link
Contributor

JulioJerez commented Mar 30, 2022 via email

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

4 participants