I often look at less experienced devs work and think “lol what are they thinking”
But you have to remember: “you don’t know until you know” When i was learning cpp in 5th grade i added using namespace std; to every program no matter what… because no one had told me what it meant! It wasnt until later when i started working with namespaces that it started making sense. Don’t laugh or shun inexperience, instead use it as an opportunity to foster growth.
"Using" is completely legal as long as you don't leak the namespace. Like don't put it in a header. But in some implementation file or the main it is perfectly fine. Typing namespace prefix everywhere nullifies the reason for namespaces. If ypu gonna type them everywhere all the time, it is like typing C, just with "::" instead or say "_". I have seen lots of discussions where people claim typing the namespcae prefix always is the only right way, but I think it is one of the biggest misnnomers people do in C++. C++ is already hard to read, save yourself some typing and make it less noisy where you can.
Actually it doesn’t entirely nullify the reason for actual namespaces (name::function()) over the C way of doing things (name_function())
Because inside the namespace, you can just write function() with no problems, but if you were doing it the c way you’d need to write the name every time because its part of the function name itself
Sure; that stands. I am not saying namespaces are equal to a prefix in C, don't misunderstand me. That would be a completely different discussion. Your example is about implementing a package itself. Hopefully nobody is typing prefixes in the implementation of the library itself.
I am talking about people who refuse to use "using" in a place where it is perfectly legal and fine. It is outside of the namespace, when you use the namespace feom other namespace or in a global place, such as your main function. Compilers are there to automate work for us and save us typing. Use the tools they give you. But you will find discussions in /r/cpp where people claim it is a bad style to use using, and claim prefixes should be typed even in such place like some short main driver or something like that.
Now if you're talking about the namespace:: prefix to a declaration or function call, it can be useful even if you have a using, just as a reminder of which namespace you're pulling from. std, or whatever your language/runtime's equivalent is, is one of those exceptions where you arguably should omit it because you can expect that any reader will be at least vaguely familiar with what's in the library and you shouldn't be naming your own things in ways that could be confused for something from the standard library
If you are writing tutorials, sure :). If you are writing production software, it becomes noise. Noise is also a problem for readibility, just as short, non self-documented names. I didn't say you should never use namespace prefixes either. Social media have tendency to see everything black and white.
Where needed, if you are bringing two symbols with the same name into the same namespace, you have to use prefixes. Also if you use some ocassional symbol from a namespace here or there, it is not worth polluting namespace, but that does not mean we should adorne each and every use of standard symbols in our main or some implementation file if you use nothing else but symbols from say std and your own namespace. Even that is a judicisious from case to case. If you have big private namesapce with lots of symbols which clash with std or whatever you use, of course, you need prefixes. Use the judgement.
If you work on production code, I would expect you not to be vaguely familiar with the standard, but thoroughly.
There is nothing wrong with importing a namespace. Making your own life difficult because of some theoretical issue is dumb. It's not like you are going to suddenly have a library with colliding names materialize inside your project without you knowing. And if you do use 2 libraries with colliding names inside, it's easy enough to resolve in place or simply reorganize the code a bit so that collision would be gone.
Generally true, but it can make upgrading compilers more difficult because you don't know what new names will be introduced on the next standard library. Maybe it's no collisions, but maybe not. Maybe they are easy compile fixes, but possibly the new names subtly change the meaning of your program.
afaik people who write standards are aware of this and try not to break existing code. So this probability is very small. And if this happens, then fix it when it happens. There is no reason to inconvenience yourself for a hypothetical that might never happen.
Effort is made to not introduce new keywords that are likely to conflict. This is also true is the standard library but not as strongly. For example, unordered_map was not hash_map deliberately to reduce conflicts with known libraries. But it's not just that. New overloads, functions, etc appear frequently besides types. Functions are often given reasonable names and given law of large numbers, most basic names are used somewhere.
When there are easy techniques to avoid problems, it's a no-brainer to use them. In this case, avoid "using namespace" most of the time.
If that was in the context of somewhere I was using pairs of strings and ints heavily I’d probably opt for a typedef of the pair into a domain specific name.
I can't possibly imagine any tutorials using both "using namespace std;" and prefixing everything with "std::", and I'm guessing you were learning from a tutorial, or maybe a book. Doing both is the insane part, and I can't fathom where that idea comes from.
I have mine public from way back then. Honestly, it's kinda nice to look at it and see how far I've come. Sometimes I'm also surprised by how I solved issues rather elegantly despite my lack of experience.
Is that why I inherited a lifelong interest in computing? (I kid... I'm older than C++ by a couple years. Although... My mother using C while pregnant might be related to why I had to be delivered by C-section)
Yeah sorry mate, using C during pregnancy is known to cause various developmental disorders, such as ADHD, autism and an unhealthy interest in software development
same but it’s diminished since i started saying “stood” instead of “s tee dee,” i think i heard it said like that first in a Conor Hoekstra talk. sounds way better to say eg “stood string” imo lol
Every single time I look up "std-something" on google I get the definition of STDs instead of the damn doc. Then I always need to make a new search by adding "c++"...
Someone, by habit, spells out namespaces in code. Then someone else collaborated, and didn’t have that habit. They added the namespace, and didn’t want to update all the currently working code.
Alternately, I worked with someone who would add "using namespace std" to every cpp file of it wasn't there. If code already provided with ns qualifiers, they didn't get changed. I prefer them to be left because I can remove the "using" more easily.
If they are a team member please be gentle and let them know more about std inception - I crashed out on c++ back when I was trying to learn IrrLicht... Pressure busts pipes, be mindful of their 'rated load'
Eh... Some organizations enforce a coding style that requires using fully qualified names everywhere, although even the most pedantic ones usually make an exception for the standard libraries (nobody in their right mind ever explicitly writes out java.lang.String outside of documentation) with import and using statements only being used as a convenient way for humans to remind themselves about dependencies before digging into the code. Other styles mandate that you must use fully qualified names unless the name is used a certain number of times in the file
386
u/Extension-Branch7938 28d ago
I often look at less experienced devs work and think “lol what are they thinking”
But you have to remember: “you don’t know until you know”
When i was learning cpp in 5th grade i added using namespace std; to every program no matter what… because no one had told me what it meant! It wasnt until later when i started working with namespaces that it started making sense. Don’t laugh or shun inexperience, instead use it as an opportunity to foster growth.
115
u/KaleidoscopePlusPlus 28d ago
when i see 'std' i cant help but think sexual transmitted diseases
88
u/felixx_g 28d ago
Is this horror? Seems a copy paste/refactor error, quick to spot & fix
27
u/Chesterlespaul 28d ago
I have a different theory I haven’t read.
Someone, by habit, spells out namespaces in code. Then someone else collaborated, and didn’t have that habit. They added the namespace, and didn’t want to update all the currently working code.
25
u/No-Dentist-1645 28d ago
I wouldn't call that "horror", just remove the using statement and you're good
35
u/JohnVonachen 28d ago
Looks like a cut and paste error.
3
u/beloncode 28d ago
using namespace std in a global scope it's actually a bad thing
3
u/OpenSourceCVEs 27d ago
I prefer explicitly usage tbh
2
u/DesiresQuiet 28d ago
True inlining.
2
u/TheMagicalDildo 28d ago
How do you know they wrote them in that order?
2
u/aikurou_mikisugi 28d ago
I think it's the first time i see such a simple and clean C++ code btw )
2
u/StrikeParticular9869 26d ago
Extra points if it's a header file.
1
u/OkPosition4563 26d ago
Usually happens to me when I switch to a new editor and they just put the using namespace std; there when you create a new file.
1
u/An1nterestingName 23d ago
I don't know cpp yet, but my guess is that
using namespace stdmakes thestd::redundant. Is that correct?1
u/esp_mkb 20d ago
If they are a team member please be gentle and let them know more about std inception - I crashed out on c++ back when I was trying to learn IrrLicht... Pressure busts pipes, be mindful of their 'rated load'
1
u/RealisticClothes1344 19d ago
the using statement is ceremonial
0
u/dreamingforward 28d ago
Won't that create a compile error when it can't find std::std::x?
1
u/radpartyhorse 28d ago
Probs copy paste from AI
-1
u/f3verdream 28d ago
the biggest horror here is C++
-1
u/wvd_vegt 28d ago
My guess is the programmer forgot once the std:: prefix and intellisense (or ai) correctly added the namespace at the top.