One thing that can become a problem for programmers is mixing Namespaces from either 2 different libraries, or 2 Namespaces from the same library (such as “iostream”). This can give a Namespace Clash that can make if hard to know which Namespace the is to be used.
Did my best Ben! Thanks Guys and Gals!
1 Like
My favorite language Pharo, has no namespaces at all, but then it has an IDE that does not allow to make names that clash but clash can exist when ones imports code.
The secret here is usually unique names, which generally means long names. Some people love them some people hate them, personally I don’t mind as long there is an IDE with efficient auto completion, if its fuzzy auto completion even better.
I usually follow Pharo convetions which means prefixing the name with initials of the name of the library for the names of the class for example lets say we have a light source the name of the class for unreal would be
UNRLLightSource
usually a 3 letter or four letter prefix will give you 1 in 17576b or 1 in 456976 chance for a name class just the prefix itself. In the above case we have 4 letters for unreal (UNRL) which gives us 1 in 456976 chance for name class and not including the rest of name of class . Total chance for clash in case of UNRLLightSource you can find by counting the amount of possible letters (26 for english language) in the power of the amount of letters used so in this case is 26^16 which makes a chance 1 in 4 followed by 22 zeros . Enormously low chance.
So you will be fine even if you decide to not use namespaces, just make sure you follow this recipe for naming. It has worked for Smalltalk (Pharo is a Smalltalk implementation) for almost 50 years now (Smalltalk was created back in 1969).
When it comes to other languages namespace is auto handled by something call package in JAVA.
Well, I like to think about this like a normal conversation between two people. In natural language there are homonym words.
For example in Spanish, the word “vela” could mean “candle”, “sail”, “stay awake”, “to guard”, and without any context it would be impossible to know which one we are talking about. I think about the namespaces as the context of a word in natural language. For example, if I’m in a (the namespace) church and I say “velas”, it’s pretty obvious that I’m talking about the candles.
But, if I’m praying at a shrine inside a sailing ship, then I’m in two different contexts with the word “vela”, so if someone comes at me and tells me “Revisa las velas”, he could be meaning “Check the sails” or “Check the candles” and I would be very confused! Then I must ask, “Hey!, Are you talking about the shrine “velas”(candles) or the ship “velas”(Sails)”.
Something like that happens when namespaces clash, the compiler would be confused and then you must tell him “I’m talking about the ship namespace!”, and then he can do the job!
Hope I’m not wrong
!
Regards!
3 Likes
Thanks for creating this board @Archangelewis. Here is my attempt:
Namespaces define the scope of functions and variables to be used for specific purposes. Sometimes different namespaces use the same names for data that do different things. If you accidentally use the same name for different functions in different namespaces without explicitly saying which one you want to use you will confuse the computer (they don’t like ambiguity) and cause an error.
Here is what I think. It would be be a similar to having two photos with the same name in different folders or albums. Conflict may occur when you call for one of these photos to be linked in an email, or blog for example. Ok so a photo links but the visual result may not be what you wanted. In order to avoid this you would need to define or name this photo uniquely. I’m not sure if I’m right here, but that’s what I’m understanding at this moment into the course.
Here is my take at explaining the problem with using includes and namespaces:
When you include a library of code, it comes with a lot of usefull functions. Some libraries are very large, and hav many functions. Some of these large libraries have been divided into “namespaces”, to categorize all the functions and so that you can use the same name for a function inside different namespaces.
It can, though, become tiresome to always write out the namespace in the code followed by :: and then the name of he function you want to use, thus one can write “using namespace name_of_namespace;” at the beginning of ones code to avoid typing “name_of_namespace::…” all the time.
If you use more than one namespace there might be functions that have identical names in the different namespaces, and thus it is unclear which of the functions you want to use both for yourself, but also for the compiler( or does it just take the first/last namespace?).
How do you avoid the clash if you use several namespaces? I don’t know
Maybe you learn the hard way which functions are named the same in different namespaces and then for those functions you always explicitly write “namespace::…” before it.
The risk of using multiple namespaces is that if there is a similar identifier (method or variables) and two or more namespaces are being used then the program may not be able to identify which definition to use. To avoid this, simply identify the namespace being used at each point, instead of the 'using ’ directive at the beginning of the file. The code won’t be as short to read and require more work, but the compiler will know which namespace definition to use for each instance. This is not an issue if you are using only one namespace of course, but if you are using multiple, making sure similarly defined items are not used is a must. I hope I explained it well. If there’s any issue with my understanding, feel free to let me know. 
I think the risks of using namespace has been fairly well explained so far by everybody. What I would like to add is that regardless of whether you include using namespace once or multiple times, it is always possible to specify which namespace the identifier you’re using belongs to.
I think a good exercise in understanding what that means is to compile your code with the line using namespace std, and to still type std::cout or std::endl. You will see that the compiler works just fine with both things existing within the code. However, it comes as no surprise that doing this is liable to cause namespace clashes anyway if you don’t fully understand why you are using a particular namespace. You don’t necessarily need to know every last identifier that exists within a namespace, but you should generally understand why you are including “using namespace” in your code.
The metaphor is seductive so thank you for it. I loved it and seemed to confirm my intuition. Has it been confirmed as being relevant ?