SFML community forums

General => General discussions => Topic started by: mweb on August 14, 2007, 11:14:43 pm

Title: why not using a namespace
Post by: mweb on August 14, 2007, 11:14:43 pm
Why did you choose to use prefixes in front of each class instead of using a namespace for the how SFML? I usualy prefer to use namespaces but maybe you got a good reason for it.
Title: why not using a namespace
Post by: Laurent on August 14, 2007, 11:34:29 pm
Not really a good reason, it's more a personal style. Anyway, I don't think this is a problem, right ?
Title: why not using a namespace
Post by: Alp on August 14, 2007, 11:45:13 pm
As namespaces are mainly used to avoid name conflicts, and as SFML classes are prefixed by "sf", I don't think that putting SFML in a namespace would be that useful ;)
Title: why not using a namespace
Post by: Laurent on August 15, 2007, 12:07:08 am
I think he suggested to use sf::Window instead of sfWindow (for example), not sf::sfWindow ;)
Title: why not using a namespace
Post by: Alp on August 15, 2007, 12:09:56 am
Indeed. I think too.
It's not really useful for the moment :)
Title: why not using a namespace
Post by: mweb on August 15, 2007, 01:42:05 am
Ok it is not necessary but the namespace was added to the c++ that the prefixes for classes are no more necessary. But since this is your project it is your decision. And as you said it is not that important. I just wanted to ask.
Title: why not using a namespace
Post by: Alp on August 15, 2007, 11:47:54 am
Maybe one day it will be necessary. We'll see :)
Title: why not using a namespace
Post by: mweb on August 15, 2007, 03:07:51 pm
I don't see a reason why it should get necessary, it is just more convenient and usually it is easier to change something if it is smaller and not bigger ;-)
We will see.
Title: why not using a namespace
Post by: Gawen on August 15, 2007, 05:54:40 pm
I agree with mweb. Namespaces had been created especially for this kind of consideration.

Indeed do not using namespace won't kill every babies seals of the world, but why do not use something especially created for that ?

I think mweb is right, it won't be necessary : that's just C++ philosophy, a C++ concept. A way to respect the reason.

Well, ok, I know that's not really important :)
Title: why not using a namespace
Post by: lubos on August 16, 2007, 09:40:53 pm
hi, new user there im gonna give sfml a try :D
personally i like namespaces more also  :P
have a nice day
Title: why not using a namespace
Post by: Alp on August 16, 2007, 10:08:01 pm
I like them too.

It represents well modules and prevent name conflicts.

But putting SFML in a namespace is not urgent ;)
Title: why not using a namespace
Post by: PM on August 16, 2007, 11:14:51 pm
Quote from: "Alp"
But putting SFML in a namespace is not urgent ;)

If putting SFML in a namespace is planned - then you should do it as soon as possible. Because if you will do it later (when there's nothing else to code) - you will create a lot of problems for the users of your library (they'll have to convert their code too, you know).

So, here's the verdict - if you're going to do it, then do it *now*. Otherwise, just tell people to ST(F)U and that you will never put it in a namespace.

Personally, I like namespaces and I use them a lot in my code.
Title: why not using a namespace
Post by: Alp on August 17, 2007, 02:15:30 am
The Dev. Team will discuss discuss about it tomorrow.

Thanks for your suggestions ;)
Title: why not using a namespace
Post by: lubos on August 17, 2007, 01:38:57 pm
let us know how did you decided  8)
Title: why not using a namespace
Post by: Laurent on August 17, 2007, 01:53:35 pm
I made this choice of not using namespaces when I started to write the library, for various reasons (mainly a personal style, there is no particular technical reason).

Now, I'm ok to change it, but as it's an important decision I need stronger arguments. Not only "it's C++ philosophy" or "namespaces are cute" ;)
Title: why not using a namespace
Post by: lubos on August 17, 2007, 02:07:27 pm
1. you can use using namespace sf( so no need to write sf everytime)
2. as i see sfml is organized into more libs, even these libs can use their own namespace like sf::audio or sf::gfx, dont know if it will be good solution thought :?
3. you can see it nicer in IDE object browser! =)
4. yes namespaces are cute :)

..sorry if i mistaked smth, i just downloaded sfml and now getting it to work on my linux  :D
Title: why not using a namespace
Post by: Aszarsha on August 17, 2007, 02:26:13 pm
The main advantage of namespace would be the Koenig Lookup !
It would allow developers to do not break C++ idioms, for example massive utilisation of swap ! Users would do not have to use something like sfSwap, which is really NOT cute, and without having to pollute global namespace with another swap overload !

As lubos said, lot's of tools use namespace for easier navigation. See the SFML doxygen generated documentation : every class begin with S, it's not a really good organisation, it's not absolutely bad, but not perfect through.
Title: why not using a namespace
Post by: PM on August 17, 2007, 03:59:46 pm
The strongest point for using namespaces is the 1st one from lubos post - if you don't like the prefix, just add using in your file and you can write code without it.

I also agree with Aszarsha, that the documentation would look nicer without the prefix.

BTW, it is quite easy to make the old code work with the namespace - all you need is a few typedefs. Of course the right thing to do is to change all of the code. But the lazy ones can simply use typedefs... :)
Title: namespaces -- my 5 cent
Post by: lordolin on August 20, 2007, 08:20:10 pm
Hello there,
since this lib is exactly what i have been looking for for quite some time,
I am really interested in making this lib as good as possible.
So here are my 5 (or whatever) cent.
(I am a professional C++ developer and have some experience with this stuff.)
Using namespaces definitely gives the whole thing a more professional appearance . Every major C++ API out there has its own namespaces and if u want this to become the next SDL maybe even used in commercial titles this is absolutely essential.
Of course, there are real, actual, tangible reasons for it:
1. write less if you use "using namespace xyz"
2. avoid collisions, even when using the namespace, by using fully qualified names.
3. it documents intent: If u have namespaces like sf::audio etc it serves well as a form of documentation.
4. makes code more navigable in IDEs.
5. etc. as has been said by others

So plz. do it and do it NOW.

regards, Oliver
Title: why not using a namespace
Post by: Tecla on August 20, 2007, 10:52:39 pm
I agree; namespaces are cleaner and have all of the advantages given above, and they would help adoption for sure.

What I suggest is moving everything into the 'sf' namespace (or even 'sfml' would be better), then creating sub-namespaces for each package, and move the appropriate types into those.  Then you can keep around a compatibility header that typedefs all of the namespace-correct versions into the old prefix-style versions; that keeps code changes to a minimum for those that don't want to have to change all the code they've already written just yet.

I would do it, but I'm in the middle of trying to move to the SF bay area to start a new job at a movie VFX studio.  :D  I am definitely interested in contributing to this project.  (I'd love to help with or see Gtk+ integration, as well as a Tao Framework package corresponding to this or some other .NET interface so you get exposure from that side of the fence.)
Title: why not using a namespace
Post by: Laurent on August 21, 2007, 08:24:02 am
Ok, it seems my library won't be as good as I want until it uses namespaces... ;)

I'll add it ASAP and make a new release.

Thank you for convincing me.
Title: Re: namespaces -- my 5 cent
Post by: ExcessNeo on August 21, 2007, 12:49:36 pm
Quote from: "lordolin"
Every major C++ API out there has its own namespaces and if u want this to become the next SDL maybe even used in commercial titles this is absolutely essential.

I may not have used SDL for awhile but last I remembered there was no namespaces needed to be used on my side. When I last used DirectX I also don't remember a namespace required to be used, and from what OpenGL code I have seen there also is not a single namespace declaration required from the API.

Bad arguement.
 
Quote
Of course, there are real, actual, tangible reasons for it:
1. write less if you use "using namespace xyz"


Hold on.....you call yourself a professional developer, yet you recommend people to do "using namespace namespace-name"? :shock:
Title: why not using a namespace
Post by: mweb on August 21, 2007, 01:06:28 pm
Quote

I may not have used SDL for awhile but last I remembered there was no namespaces needed to be used on my side. When I last used DirectX I also don't remember a namespace required to be used, and from what OpenGL code I have seen there also is not a single namespace declaration required from the API.

Bad arguement.


Sorry SDL is C code the same goes for OpenGL. Namespaces are a C++ construct so do not mix up languages please. I can't talk about directX since i don't use it.

Quote

Hold on.....you call yourself a professional developer, yet you recommend people to do "using namespace namespace-name"? Shocked


whats wrong with using namespace?
Title: why not using a namespace
Post by: lubos on August 21, 2007, 01:29:11 pm
yup directX is c++, and no namespaces there(iirc)..
this is going to be war.. prepare to battle :D
Title: why not using a namespace
Post by: mweb on August 21, 2007, 01:55:23 pm
better don't start a war, I'm not sure if anyone wants to use DirectX as a good example how a lib should look like  :wink:
Title: why not using a namespace
Post by: lubos on August 21, 2007, 02:23:50 pm
Quote from: "mweb"
better don't start a war, I'm not sure if anyone wants to use DirectX as a good example how a lib should look like  :wink:

yep, peace, but look at most of the modern commercial games out there..its directX  :cry:
Title: why not using a namespace
Post by: mweb on August 21, 2007, 02:28:49 pm
yep your right, its widely used, but also widely hated for its API ;-).
Title: why not using a namespace
Post by: PM on August 21, 2007, 04:14:48 pm
Quote from: "mweb"
Quote

Hold on.....you call yourself a professional developer, yet you recommend people to do "using namespace namespace-name"? Shocked


whats wrong with using namespace?

It is only wrong if you add it somewhere at the top of a header that everyone else will include a lot. There is nothing wrong with using namespace in cpp files (file scope) or better yet - inside classes or functions.
Title: why not using a namespace
Post by: Tecla on August 25, 2007, 08:22:21 am
Yeah, generally the rule of thumb is to not put 'using namespace blahblah' into global scope, unless it is in a cpp file (and even then you might consider only doing it in function or class scope as suggested by PM).

On a game project I'm working on at a certain huge game development publisher, that's the rule we adopted for our team.  We have mandated that we namespace-ify all of our code, but we have forbidden 'using' statements in header files except where you can restrict it to a specific scope.  All of the intellisense-type tools and whatnot then make navigating namespaces to find identifiers really easy, and it makes for quick identification of where code logically fits into modules and sub-modules.

For C APIs, they use prefixes on their types and functions because, well, there weren't namespaces to do that job even better.  For DirectX, it was first developed back when there was no namespace support in C++, and worse, it had to conform to the COM interfaces (which probably didn't easily allow themselves to be put into namespaces, at least when they were first introduced to Visual C++).
Title: why not using a namespace
Post by: PM on August 25, 2007, 08:52:17 am
Quote from: "Tecla"
For DirectX, it was first developed back when there was no namespace support in C++, and worse, it had to conform to the COM interfaces (which probably didn't easily allow themselves to be put into namespaces, at least when they were first introduced to Visual C++).

I remember doing some research about COM. The main idea about it is that you can create a COM object in any language that supports COM, and then you (or others) can use that object with any language that supports COM. And there is actually a lot of languages that support it - C++, C, Assembler, Pascal (Delphi) and many more. So, to make it work on all of the languages, they had to skip support for some features (like namespaces).
Title: why not using a namespace
Post by: TriKri on May 30, 2010, 09:38:08 am
I'm just wondering why the main namespace is called sf and not sfml? sfml is more descriptive in my opinion.
Title: why not using a namespace
Post by: Laurent on May 30, 2010, 10:19:51 am
Because it's shorter, which is crucial because it prefixes every type name.
Title: why not using a namespace
Post by: TriKri on May 30, 2010, 02:09:09 pm
Ok.