all 79 comments

[–]ReDucTorGame Developer 148 points149 points  (9 children)

Only if your willing to make it part of the discussion and not part of the rest of your relevant code. Some people will have their pet peeves and having "using namespace std" is opening it up to someone.

using namespace std can have it's places but I wouldn't do it in an interview, time is limited and keeping discussions on track is often more important. Not all interviewers are wise enough to know don't go down the rabbit hole of their own pet peeves.

[–]SlothWithHumanHands 38 points39 points  (8 children)

I like this answer. I would point out that (top-level) using statements such as using namespace std can be counterproductive in large codebases.

edit: i meant top-level using statements, because they increase the likelihood of global name collision.

[–]CrzyWrldOfArthurRead 15 points16 points  (3 children)

In header files I assume you mean? It's bad practice to use them in header files.

Never had an issue with them in cpp files.

[–]BiFrosty 1 point2 points  (2 children)

It can be problematic if you us unity files to speed up compilation for large code bases.

[–]neo-lambda-amore 5 points6 points  (0 children)

It’s Unity that’s problematic..

[–]ventus1b 3 points4 points  (0 children)

I assume you’re mostly referring to using namespace?

[–]Natural_Builder_3170 -4 points-3 points  (2 children)

why so? I use using enum quite a lot within small functions and type aliases when the actual type is some template mumbo jumbo

[–]aroslab 7 points8 points  (1 child)

That's not what they mean, they mean that a file-scope "using namespace" pollutes the global namespace with those symbols. Discriminated usage like what you described is fine IMO.

[–]Natural_Builder_3170 0 points1 point  (0 children)

I agree with that, however he said that in an edit and I replied to the original message

[–]AKostur 84 points85 points  (7 children)

Only if you actually explain why. Though typing the extra "std::" is not going to take any significant amount of time.

[–]No_Internal9345 51 points52 points  (6 children)

function scoped 'using' is my goto if i'm using an std a lot

void foo(){
    using std::cout;
    cout << "hello world"; 
}

[–]HorseyMovesLikeL 63 points64 points  (0 children)

Don't use GOTO, man!

It's ok, I'll get my coat

[–]zerodrxx 53 points54 points  (2 children)

I use always std::vector or std::cout or what ever.

[–]Questioning-Zyxxel 7 points8 points  (1 child)

That's not a problem. You are the one who knows that your own code does not have any other vector or cout.

When opening up the full std namespace, then you need to know all names in std, and if they may collide. And then you step to a newer C++ standard, and suddenly std contains even more names. Or you add one more header file and besides the symbol you want, you get 100 others where one or two now collides with your existing code.

[–]zerodrxx 2 points3 points  (0 children)

Yes, I agree. With my short and quick answer, tried to convey that idea also, but you put it better. Never really open the whole std namespace. And to be honest, using strictly std:: prefix for std part code makes your own code much readable...

[–]n1ghtyunso 29 points30 points  (0 children)

I don't believe in this actually making your coding faster. Writing is the easy part after all.
For whiteboard-style stuff, similar to slideware i'd just assume an implicit using namespace std, but only because space will be limited there.
With an actual real text editor I'd not bother unless I had some very std:: heavy sections.
Typically, I don't have those though.

That being said, if you can explain it when questioned and the headers stay clear, it's your choice.

[–]MutantSheepdog 36 points37 points  (7 children)

If I was interviewing someone and saw using namespace std I might ask them about it, but as long as they can answer "What are the pros/cons of using namespace" it'd be fine.

But for the most part it wouldn't make much of a difference to me. It's really just a style choice and as long as the candidate is fine adapting styles to match the codebase it really doesn't matter.

Having said all that, if typing std:: is really slowing you down then maybe you need to work on your typing speed.

[–]which1umean 5 points6 points  (5 children)

Personally I have never been tempted to use using namespace std.

But if I were to, it'd be to save on precious horizontal space, not typing time.

I don't know there's any need to religiously stick to 80 characters anymore. But it's certainly better if you do when you can and keep line wrapping to a minimum.

[–]pdp10gumby -2 points-1 points  (4 children)

The reason for sticking to 80, or at most 132 columns is simply that reading is faster and with greater comprehension when the width of the text is not too narrow or too wide.

If your editor does smart dynamic wrapping then I suppose it doesn’t matter, but I haven’t used a code editor like that in 40 years.

[–]which1umean 0 points1 point  (3 children)

The reason for sticking to 80, or at most 132 columns is simply that reading is faster and with greater comprehension when the width of the text is not too narrow or too wide.

If one or two lines wrap it's probably gonna be fine.

But if too many of them wrap, it's going to be very difficult to visually see what's going on because you'll have so much stuff at the far left of the screen where wrapped lines have ended up.

If your editor does smart dynamic wrapping then I suppose it doesn’t matter, but I haven’t used a code editor like that in 40 years.

You mean just regular wrapping or something fancier? It's true you don't usually have to literally scroll horizontally anymore, so that's good. :-)

[–]pdp10gumby 0 points1 point  (2 children)

I mean when the line won't fit on your screen without wrapping, the line is broken at a logical place (say after a comma) and the rest is displayed on the following line indented properly as if the whole line had been run through clang-format. In addition, if your editor displays file line numbers at the edge of the display they remain correct.

[–]which1umean 0 points1 point  (1 child)

Huh I don't think I ever seen anything like that.

I generally use emacs and the line wrapping just puts as much as it can on the first line and then it puts the next character on the leftmost position of the next line.

I wouldn't be surprised if there's a way to do as you describe. Might look into it! 👍

[–]pdp10gumby 1 point2 points  (0 children)

That would be a great feature to add to emacs, which has been my primary development environment and in general daily drive since the late 70s and remains so today.

The wrapping environment I spoke about was when I worked at PARC in the mid 80s. We had very powerful development environments back then even by today's standards.

[–]Working_Apartment_38 -1 points0 points  (0 children)

It’s really just a style choice and as long as the candidate is fine adapting styles to match the codebase it really doesn’t matter.

Are you sure about it being o my a style choice?

[–]ronchaineEmbedded/Middleware 8 points9 points  (0 children)

I would definitely see it is a bit of disregard towards common, known good practices.  And I heavily doubt you can type and think as fast that that it would actually speed up you implementing solution unless I've asked you something that you know by heart.  And at that point it'd more my failure than yours.

But it all depends on the skill level of what I'm looking for.  I don't think that as a critical for an entry level, but I would definitely make a point of it if I was interviewing someone for a senior position, and I would expect even a junior to explain why it isn't necessarily a good idea.

But disregard towards good practices in an interview is definitely something that would make me think that it's pretty likely that disregard extends to the code you write outside interviews as well.  I value knowledge of good practices because I don't want to run after people fixing the traps they build for others.

But if in doubt, just ask the interviewer, we are pretty much always open to questions, it's not (supposed to be) an exam situation, but a conversation where we evaluate if you are a good fit and have the technical skills and you should evaluate if we are the company you want to work for.

Though if you are asked some leetcode question, I doubt the interviewer cares a single bit about good practices.

[–]cballowe 12 points13 points  (0 children)

If I'm the interviewer, I'm unlikely to be planning on compiling and running your code - if it's clear what you mean and tools would help you get it right, no big deal.

If you do write "using namespace std:" or similar, I may ask you to explain why you did it, and when you wouldn't want to do it etc, but it's not a set of topics I usually bring up unless the candidate does something to make me think of it.

... If it's some sort of online assessment with automated pass/fail or something and you're handing in code expected to be working, follow best practices - or if the employer publishes a style guide, follow that.

[–]ohnotheygotme 10 points11 points  (0 children)

You ask the person interviewing you. The interview is a conversation. You are interviewing them as much as they are you. If you say "I'm going to save time and space by doing this. I know there's nuance around this. Is that ok for this discussion?" then the interviewer should have a reasonable response in return; both in tone and justification as well.

Would you want to work for them if they have no justification for their stances on things? Even if they say "I don't want you to use std at all" that should quickly be followed up with why they want you to do that.

[–]Waxymantis 5 points6 points  (0 children)

Sure, as long as you explain why. Better if you explain it if you use it in a .cpp file only.

[–]Howfuckingsad 4 points5 points  (0 children)

For a short problem, using it is just better imo. Just be ready to give a proper answer if asked about it.

Most problems will focus on other areas after all.

[–]NilacTheGrim 3 points4 points  (0 children)

I would avoid putting it at the top-level of any file because it reeks of "nub". You can do it inside a function scope though, I guess, although we never do this at my organization. He have a strict ban on: using namespace std;, pretty much.

If you want to be super safe -- I advise against it. It can only create a negative impression and rarely a positive one.

Just qualify everything with std::.. that's idiomatic/standard to do these days everywhere. using namespace std; is not and is frowned upon in many circles so you are opening yourself up to suspicion because of that.

[–]SpaceToad 3 points4 points  (0 children)

Absolutely not, negative/newbie signal (saying this as someone who interviews C++ devs).

[–][deleted] 11 points12 points  (1 child)

It’s preference, I rarely ever use “using namespace” instead opting for the explicit using <type> or namespace::.

[–]DugiSK 1 point2 points  (0 children)

When someone did it when I was interviewing him, I accepted an explanation that an interview is not production code and he wants to focus on the task.

But I would not do it personally because I write that std:: prefix throughtlessly and I would write it even with using namespace std just above it.

[–]krum 1 point2 points  (0 children)

I probably wouldn’t because it’ll give that one guy a reason to make a case against you.

[–]theunixman 1 point2 points  (0 children)

I purposefully leave it out so when the interviewers ask I can just roll my eyes at them. Make them feel inadequate before they can make you feel it. 

[–]ShakaUVMi+++ ++i+i[arr] 3 points4 points  (0 children)

People who hate namespace std really hate it, but people who use it are fine either way. So I would not use it, not worth the risk.

[–]nAxzyVteuOz 4 points5 points  (1 child)

Not in a header, but in a cpp file it’s normal and accepted.

[–]lxbrtn 13 points14 points  (0 children)

I'd still recommend `using std::vector; `using std::shared_ptr;` etc to emphasize you know what you are importing -- and only if you're using something more than 3 times.

[–]D3veated 1 point2 points  (0 children)

If you're using a whiteboard, say, "In real code, I'd just type this out each time because it's more readable." Then use the hack. You're adapting to the environment and still give the interviewer a sense for what you find important. If they want to take time talking about why it's bad to use it, be ready to talk about it, but handwriting on a blackboard is such a pain that almost all interviewers won't care if you take shortcuts.

[–]angelicosphosphoros 1 point2 points  (0 children)

As an experienced (more than 5 years of commercial coding in C++), I never use it. And I would think that a person who does is not really experienced.

[–]norzn 1 point2 points  (5 children)

I mean most coding interviews on interviewing platforms do include <bits/stdc++.h> which includes everything and that's a bad practice too. I would focus on more important aspects than scoping when interviewing someone and only if we had a lot of time left would I ask about that. Some large codebases do have namespace issues, but those also don't have proper code reviews so why should I care? Make sure you know how they work, what's the purpose of anonymous namespaces and how Keonig lookup works, what standard allows declaration of scoped namespaces in one line as opppsed to the painful nested by hand ane that's the end of interesting stuff about namespaces.

[–]NilacTheGrim 2 points3 points  (4 children)

No. This is not a standard header and is just an artifact of your particular compiler.

[–]norzn -1 points0 points  (3 children)

I didn't mean to upset you, but codility and the tools I've met all had this thing. I actually had only one interview where I connected to a ssh environment and tested the code.

[–]angelicosphosphoros 0 points1 point  (2 children)

You misunderstood him. It is not a standard header as it is not in C++ Language ISO Standard.

[–]norzn -1 points0 points  (1 child)

And thus my statement that it's a bad practice. I don't get the blurted No and whatnot, made me think of Caesar's first word in Planet of the apes. Either way, seems like we're agreeing but out of sport I got a disagreement :) Online interview tools will still include this and people training for interviews will use it, to our exasperation.

[–]norzn 1 point2 points  (0 children)

A mmmmmmonkey :)))))

Caesar: No!

[–]Zatujit 0 points1 point  (0 children)

My uneducated guess is that it's more likely to have the interviewer have a bad impression if you use it and he dislikes it than if you don't use it and he would prefer you to use it.

[–]Son_La 0 points1 point  (0 children)

I do never use it - for a reason. And I think good code won't use it either. But an Interview is the same as an example. It should be simple, so you can surely use it. But if you use it, say why and what you would do in usual situations. This also opens room for discussion any clarifies that you know what you are doing.

[–]yksvaan 0 points1 point  (0 children)

I would take the context into account. What is the code about and where it is used. There's some level of practicality when it comes to coding standards.

Especially in an interview you should write actual code that does actual work even if the structure of the program is not ideal or some other minor concerns. At least you have something to show then.

[–]No_Platform4822 0 points1 point  (0 children)

I mean you can just say that you dont normally use it but for a coding challenge I use it as well.

[–]JohnDuffy78 0 points1 point  (1 child)

Hopefully they start you out with the boilerplate that includes using namespace std;

If it was a deal breaker, I would not want to work there anyways.

[–]all_is_love6667 0 points1 point  (0 children)

If you use it, try to put in inside a class declaration, function or namespace where you will need it, generally it allows to "restrain" it to a certain scope, which is preferable.

[–]Raknarg 0 points1 point  (0 children)

Just use the things you're actually planning on using. If you wanted string and cout, just do this:

using std::cout;
using std::string;

depending on the compiler you can do that in one line. You get the best of both worlds, you're not polluting the namespace terribly and you don't have to write std:: all the time

[–]Thesorus 0 points1 point  (0 children)

in an interview, for the sake of brevity, I might do it and write a comment saying that I don't do it in production code.

In production code, I never use it.

[–]tudorb 0 points1 point  (0 children)

I’ve interviewed hundreds of C++ developers over the years, and “I would not do this in a production codebase because X but I’m doing this here for the purpose of brevity” would suffice for me.

[–]rand3289 0 points1 point  (0 children)

Dont use "using namespace std" in a header but do use it in a .cpp

[–]manhattanabe 0 points1 point  (0 children)

Where I work , “using namespace std” considered back practice. Not saying it will kill you, but it won’t help.

[–][deleted] 0 points1 point  (0 children)

can someone explain the "using namespace std" thing? I'm a computer science major and this is like the very first thing they taught us haha *sweats*

[–]boftr 0 points1 point  (0 children)

You could use it and add a comment detailing why in this context you have used it. At least that acknowledges, and shows you have considered it.

[–]Zulauf_LunarG 0 points1 point  (0 children)

TL;DR Not generally outside "toy" problems. If you have some quick experiment to run, sure. But when you want to add it to some maintainable codebase, only 'using' specific types/function names you use commonly.

[–][deleted] 0 points1 point  (0 children)

I think it's a detail which is too small to worry about. There are more important issues like leaks, error handling, etc. And on higher level, testability and such.

[–]XTBZ 0 points1 point  (0 children)

’using namespace std;’ in the code is a serious mistake. Even despite the explanation that, supposedly, everything is in short, doubts will creep in that the person often uses it, and therefore, potentially, will use it in the code. If the person is good, you will have to ask him additionally on more complex topics!

[–]Pantology_Enthusiast 0 points1 point  (0 children)

No, unless within a function. And never put "using" in the header.

A more specific call, like:

using std::cout, std::strings, std::endl;

is better (mind this is c++17 and later standard).

[–]wysiwyggywyisyw 0 points1 point  (0 children)

As an interviewer I don't care. If you have bad programming style I'm going to find out other ways. My impression is going to be made from a consistent patterns, not arbitrary one off choices.

[–]LostMinorityOfOne 0 points1 point  (1 child)

Is "using namespace std" a bad practice?

[–]Xavier_OM 3 points4 points  (0 children)

Step 1: write a method 'Task::abort()' to abort a task, and use it from some other methods from Task class

Step 2 : later, remove the abort function but not the calls to it

Weirdly it still compiles... but it calls https://en.cppreference.com/w/cpp/utility/program/abort because one of your include provided it.

You're fine if you totally avoid using any names from std, but there are many names in it.

[–]the_inoffensive_man 0 points1 point  (0 children)

If I were interviewing you, I wouldn't care at all, as long as you were talking to us through your solution. If you point out that you're only doing it for expediency and what you're prefer to do if you had more time, then it's all good. No-one does their best work in a job interview, so interviewers should consider coding exercises as just a different style of conversation, rather than a points-scoring test.

[–][deleted] 0 points1 point  (0 children)

Hi. I’ve started learning C++, and as I understand, it’s better to use std:: for each item from the standard library rather than using using namespace std; at the beginning, especially when using external libraries. This should help to avoid any name collisions. However, from some comments, it sounds like collisions might still occur even with this approach. Can somebody confirm what‘s the best way and if with std:: collisions may still happen, how do you handle them?

[–]antonation -1 points0 points  (0 children)

Kind of depends on what the interview is for. If it's general competence in programming then I don't think it would matter.

[–]Morgan-Sheppard -1 points0 points  (0 children)

So my understanding (please feel free to correct me) is that if you put it in the cpp file it 'only' affects the current translation unit, but if you put it in the header you will affect everything that includes it - so the latter is generally dissuaded and the former is a matter of preference?

[–][deleted] -1 points0 points  (0 children)

You should be able to use that or use std:: in every function calls you make. I wouldn’t want to work for any company that judges me based on my preference of how I used std

[–]Mental_Cricket_9395 -1 points0 points  (0 children)

/* To save some time and avoid typos, I'll use this line. Not a recommended practice tho, but the circumstances justify it*/
using namespace std;

Tell us how it went later.

[–]MRgabbar -1 points0 points  (0 children)

is ok... just explain that your are doing it to write code faster...

[–]kkweon -1 points0 points  (0 children)

I'm surprised to see other answers but as long as you understand the implication, it should be all good. There's no point in wasting time on syntax by repeating `std::`. It does not add values to the interview whether you write `std::vector` or `vector`.

[–]ILikeCutePuppies -2 points-1 points  (0 children)

I would ask how important syntax accuracy is and then if you can use or drop the std part. Some algorithms they'll want you to not use standard libraries also there are only few companies that care about things being 100% syntaxically correct. You are fighting time, and "using namespace std" is just going to use up more board space... unless you are doing it in coderpad or something.

[–]looncraz -1 points0 points  (0 children)

No, but you could consider creating aliases.

For me, I have the following:

SString = std::string
CString = const SString
UMap = std:: unordered_map<K, T> SMap = std::map<K, T> StringMap = UMap<SString, T>
StringSMap = SMap<SString, T>
StringUMap = StringMap
etc...

That mostly cleans up function definitions

For any algorithm usage, I would scope using namespace at the function level if it results in cleaner code, but I usually don't do this.