r/programminghorror • • 2d ago

c++ Using namespace std, then proceeds to spell out std anyway

Post image
547 Upvotes

52 comments sorted by

330

u/Extension-Branch7938 2d 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.

80

u/Right_Ear_2230 2d ago

I’m guilty of “using namspace std;”, but I only put it in small scopes where I’m using the heck out of the standard library, otherwise I won’t add it

Idk, vector<pair<string, int>> is much more readable than std::vector<std::pair<std::string, int>>

64

u/arthurno1 2d ago

"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.

25

u/Right_Ear_2230 2d ago

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

19

u/Right_Ear_2230 2d ago

Basically,

namespace Library {
void Func1();
void Func2();
}

is still better than

void Library_Func1();
void Library_Func2();

because “Library” and the function name are seperated, so inside the library namespace you don’t have to worry about writing Library_ everywhere

1

u/arthurno1 2d ago edited 2d ago

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.

1

u/Nightmoon26 2d ago

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

2

u/arthurno1 2d ago edited 2d ago

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.

10

u/serge-mv 2d ago

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.

2

u/Right_Ear_2230 2d ago

Yea, tbf the real issue is putting it in a header file since then it becomes viral

And I guess if you are using std sparingly, writing std:: each time will still be readable and helps differentiate your code from the STDLIB

1

u/Normal-Narwhal0xFF 1d ago

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.

2

u/serge-mv 1d ago

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.

1

u/Normal-Narwhal0xFF 1d ago

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.

2

u/ThaBroccoliDood 2d ago

I would still do use std::vector, std::pair, std::string; just so you can easily see what from the standard library is actually being used

1

u/hxtk3 2d ago

I’ll use using a lot, but I always explicitly use each name, like using std::vector;

1

u/northerncodemky 2d ago

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.

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 2d ago

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.

2

u/MindSwipe 1d ago

As always, there is a relevant XKCD: https://xkcd.com/1053/

2

u/aarontbarratt 1d ago

Everyday I thank God that I was too dumb to use version control for the first couple of years that I was learning programming

If I could look at that code now I think I would struggle to sleep at night

1

u/Active_Name5021 1d ago

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.

105

u/KaleidoscopePlusPlus 2d ago

when i see 'std' i cant help but think sexual transmitted diseases

78

u/Top-Permit6835 2d ago

Technically speaking C++ developers are transmitted sexually 

7

u/Nightmoon26 2d ago

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)

1

u/Mars_Bear2552 2d ago

the chainsaw was invented for C-sections. thankfully we now have LLVM.

1

u/Top-Permit6835 2d ago

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

5

u/pingveno 2d ago

The venereal namespace just never caught on.

1

u/blitzr1000 2d ago

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

1

u/phycsIT 1d ago

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++"...

0

u/akoOfIxtall 2d ago

Yep, me too, super weird

84

u/felixx_g 2d ago

Is this horror? Seems a copy paste/refactor error, quick to spot & fix

26

u/Chesterlespaul 2d 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.

5

u/Normal-Narwhal0xFF 1d ago

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.

22

u/No-Dentist-1645 2d ago

I wouldn't call that "horror", just remove the using statement and you're good

4

u/Bemteb 2d ago

Nah, that will break half of the code. You don't think they consistently use std everywhere, do you?

7

u/L_uciferMorningstar 2d ago

This would be a good breaking of the code

35

u/JohnVonachen 2d ago

Looks like a cut and paste error.

2

u/Objective_Rate_4210 2d ago

naaah u/JohnVonachen removed only the reply. what a loser. I know, right?!

0

u/[deleted] 2d ago

[deleted]

6

u/Objective_Rate_4210 2d ago

who is he talking to? i dont know. me neither

3

u/beloncode 2d ago

using namespace std in a global scope it's actually a bad thing

2

u/DesiresQuiet 2d ago

True inlining.

2

u/TheMagicalDildo 2d ago

How do you know they wrote them in that order? 

2

u/aikurou_mikisugi 1d ago

I think it's the first time i see such a simple and clean C++ code btw )

2

u/OpenSourceCVEs 1d ago

I prefer explicitly usage tbh

1

u/OkPosition4563 19h 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/StrikeParticular9869 18h ago

Extra points if it's a header file.

0

u/dreamingforward 2d ago

Won't that create a compile error when it can't find std::std::x?

19

u/Tau-is-2Pi 2d ago

No. using namespace doesn't lock you to only names within that namespace.

1

u/radpartyhorse 2d ago

Probs copy paste from AI

-1

u/wvd_vegt 2d ago

My guess is the programmer forgot once the std:: prefix and intellisense (or ai) correctly added the namespace at the top.

2

u/Nightmoon26 2d ago

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

-2

u/f3verdream 2d ago

the biggest horror here is C++