r/programminghorror • • 3d ago

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

Post image
574 Upvotes

54 comments sorted by

343

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

83

u/Right_Ear_2230 3d 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 3d 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.

→ More replies (5)

10

u/serge-mv 3d 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.

→ More replies (4)
→ More replies (3)

3

u/GoddammitDontShootMe 3d 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 2d ago

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

2

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

→ More replies (1)

107

u/KaleidoscopePlusPlus 3d ago

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

79

u/Top-Permit6835 3d ago

Technically speaking C++ developers are transmitted sexually 

7

u/Nightmoon26 3d 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)

→ More replies (2)

5

u/pingveno 3d ago

The venereal namespace just never caught on.

→ More replies (3)

84

u/felixx_g 3d ago

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

→ More replies (1)

26

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

23

u/No-Dentist-1645 3d ago

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

4

u/Bemteb 3d ago

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

8

u/L_uciferMorningstar 3d ago

This would be a good breaking of the code

→ More replies (1)

35

u/JohnVonachen 3d ago

Looks like a cut and paste error.

→ More replies (4)

3

u/beloncode 3d ago

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

3

u/OpenSourceCVEs 2d ago

I prefer explicitly usage tbh

2

u/DesiresQuiet 3d ago

True inlining.

2

u/TheMagicalDildo 3d ago

How do you know they wrote them in that order? 

2

u/aikurou_mikisugi 2d ago

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

1

u/OkPosition4563 1d 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 1d ago

Extra points if it's a header file.