Why I don't "using namespace std"
Vložit
- čas přidán 5. 07. 2024
- Follow me here! ► / thecherno
Patreon ► / thecherno
Twitter ► / thecherno
Discord ► thecherno.com/discord
Series Playlist ► thecherno.com/cpp
Thank you to the following Patreon supporters:
- Dominic Pace
- Kevin Gregory Agwaze
- Sébastien Bervoets
- Tobias Humig
- Peter Siegmund
- Kerem Demirer
Gear I use:
-----------------
BEST laptop for programming! ► geni.us/pakTES
My FAVOURITE keyboard for programming! ► geni.us/zNhB
FAVOURITE monitors for programming! ► geni.us/Ig6KBq
MAIN Camera ► geni.us/t6xyDRO
MAIN Lens ► geni.us/xGoDWT
Second Camera ► geni.us/CYUQ
Microphone ► geni.us/wqO6g7K
Finally got around to answering this question, hope you guys enjoyed the video!
What's the biggest issue you've had because someone stuck a using namespace somewhere?
First time i learned c++ the tutorial i watched the guy "using namespace std" and other namespaces, so i assumed it is ok. Couple of hours later, i actually added something like that in a header file, didnt mind. A bit later, everything broke. Needles to say i stopped doing this.
Tron I restrained from using it in a global scope, but I still use it in a local scope, because I don't see problems with that personally. But everyone has their own religion here.
In my algorithms class, the test runner that was supplied to the class had `using namespace std;` in the global scope in one of its header files, and as a result when I did the quicksort assignment I discovered minutes before the assignment was due that I'd actually been calling std::sort for everything when I'd been testing it earlier, rather than the function I'd written. Fortunately my sort function worked perfectly once I fixed that but it really could've been bad.
Im glad someone is finally addressing this. I've been helping people program in C++ for the last 5-6 years , and the one thing i notice is people always tend to use namespace std. There arent many tutorials that actually tell people not to use it because it could cause a lot of bloat. Ive actually was working with someone on a game, and it literally screwed with my custom vector class as well as custom map class.
I write a C++ program and broke every. Worst part is that I couldn't trace the issue at all and had to actually look on StackOverflow to realize that it was because of using namespace std.
When he says "never ever, EVER, use namespace in a header file" you can see the culmination of several hours of debugging agony in his eyes.
Thats solid advice. I can feel it.
My college does all the time lol
@@AviPars ur college should be in hell rn tbh
Zooming into those eyes and flashing through a thousand memories from 2am would've been a masterful edit for sure.
There a guys who start their video with an ad. And there is cherno, who shows how to make coffee in slowmo instead
Excuse me , but obviously his name is "A Jenner" according to (closed caption).Better to use subtitle options (CC button over there).
As someone who has never before heard about this channel, I was very confused
I just want a list of ingredients that he puts in his latte. I want to talk that fast and type that fast and recall all the language details that fast. It must be the latte because something is giving him superhuman brain function!
Also I suggest that Cherno puts a few drops of his latte in the cactus plant every video. I suspect that the cactus will grow to be 6ft tall and form ears, eyes and a mouth and have an IQ of about 200. My IQ measured by Mensa is 145 but it could sure use a boost! lol
ward
“Never EVER use ‘using namespace’ in a header file. “ Noted.
Learned this the hard way
You can if you don't use other libraries like he does, it would save you a bit of time. Although, it would raise a bad habit.
If you in contest about Algorithm (like ACM ICPC, ...) then you should use "using namespace std", else you shouldn't use it.
@@JoseGonzalez-rt5fk You can if you know for an absolute fact that the software will never be developed upon again after version 1. If it's for a customer; don't, you don't know what they're going to do in the future.
If it's for your company; don't, you'll be the one that has to deal with it later because they wanted to use that one feature you need a lib for.
If it's a personal project you might open source some day, also don't.
Like there are very few cases where you could, but probably still shouldn't.
Ok, but did you understand why?
You don't have to bring the entire std namespace. I mostly use:
using std::cout;
using std::endl;
and using std:cin;
lol didnt even know I could do that...
new to c++
You probably shouldn't use std::endl much at all
@@DarkLevis I mean, if you're just outputting variables or something, it's easier than typing '
'. What's wrong with endl?
@@zeyode nothing horribly wrong just it calls std::flush after '
' which is often not intended.
You know that you can write "using namespcae std" instead of writing std::, it will make a lot of the examples in this video clearer to read and understand.
but the using keyword is so situational it's not really worth it. It also becomes much less clear if you "using" more than one namespace.
...that was a pretty clunky sentence.
The only thing that you get by "using namespace std" are broken code samples which you have to go over and add std:: everywhere after you pasted the code.
It was a joke xD
That's not true. Any experienced c++ developer is completely familiar with anything inside the std namespace. The problem is with people (like he mentioned in the video) who try to replicate the STL and give it the exact names as the original. Yeah, that's gonna fuck everything up. There is absolutely no reason to std:: everywhere when you know what's what. All it does is unnecessarily bloat code. I'm not say don't not use it, but don't exaggerate it's limited consequences, which when they do arise, are easily solvable with any decent IDE.
I use condoms. So there.
The reason this question gets asked a lot is because programming courses don't want to have to explain scope and scope resolution early on, and once they do, using namespace std is so ingrained they don't bother undoing it.
Great observation!
That shit happened to me when I started lol
My teacher didn’t even know about unique scope name resolution lol. They’re too caught up in C World.
True, it's same also in universities with fields not related to the programming directly. Like we have C++ course on our first year on mechanical engineering, and we were told write using_namespace as mandatory and never got explained what it actually is, because we always used to work with console output, never used more than 2 extensive libraries (vector and cmath) and nor wrote more than 800 lines of code. Thanks to the Cherno I've learned not to do it
Thankfully my teacher explained why using namespace std is not good on the second day of the class. Even though most of us don’t understand what he’s talking about at that time.
“Remove all the STDs.”
Why I don't "using namespace std"
"latte art could use a bit of work but really hits the spot"
-TheCherno
Will read your comment while he said that.
I'm kind of an old timer. Been programming since '81. I've even programmed on a computer with real magnetic core memory. You could open the box up and see each bit.......no joke..... In any case, you're one of the few CZcams programmers I like. Keep up the good work.....
Wow, I first played with QBASIC, Assembly, etc in 1993 and I thought those were the old days. Lol. Makes me feel young. ;)
I envy y'all early programmers. You had it cooler back then
@@_lapys ikr, people that time had less resources to learn from, i respect old programmers, they r rly hardworking
wow...
would you say that programming was easier (or at least simpler) back then when the machines were less complicated and things were less abstract?
are you guys insane, today any 9 years old can download a radom App (for him) a radon tutorial on the internet and star programming on the get go, and in a couple of weeks he already know how to do lots of simples algorithms.
back them you literally need to have electronics degree to understand whatever the fuck that maching were doing or else everything would break.
longest. intro. ever.
This guy clearly does not listen to Pink Floyd.
@@IsakFyksen lol
best. intro. ever.
Yeah but i loved every second of it
so you are new here
Now we all know where the Patreon money is going towards lmao.
Probably his EA money to be fair
Going to Costa Rica cofee plantations
Making good content ?
Dude! Your videos are highly appreciated. I'm a CS student and I find your "real world" perspective on things to be extremely helpful.
"What you may have noticed at the beginning of this video"
You making coffee?
I'll sneak into your codebase and add "#define eastl:: std::"
You're like hitler, but even hitler cared about Germany or something
@@kylecox5127 lol
Then suddenly all EA games will become free and slower xD
(IDK if that makes sense or not. all I am saying is EAAAAAA = PAYYY and stdlib = slow lib!!)
@@kylecox5127 Lol nice reference
monster :0
I’ve been programming for 20 years and based on this experience, I always explicitly namespace (well, typically aliasing it, if its longer than one word), in every language I use (C++, Clojure, Rust, Python and Java). The reason is that this way I always know where a symbol is coming from, at a glance. It lowers my cognitive overhead, which speeds me up and helps me make fewer mistakes. Nevermind name clashes, clarity is more important. In the case of std, I also like to know if something is from the standard library (or other “core to my codebase” libraries like glm) or if its from my own codebase. I find knowing where something comes from cleaner than never knowing where something is declared, without thinking or checking.
Also: “vector vector;” vs “std::vector vector;” 😛
tbh rust does it pretty well by letting the user to pick which symbols they want to merge into the current scope. `use std::{cout, endl, vector, function}`;
@@liam8398C++ has using std::cout; too, it only lacks that neat {grouping} syntax
We are in 2019 and your series still rocking. Your high quality videos and all the many advice you give are invaluable, because it is coming from years of practical experience in this industry. Keep up the good work.
2020 :)
2021, still top tier
2022!
2023...
I dont know the basics of C++, I have zero programming knowledge and yet I understood what you were trying to tell. My mind is blown away by how great you are at teaching/explaining stuff to others!
BEFORE ANYONE ASKS: I accidentally saved this video in my Watch Later and I was confused how/why it was there so I decided to check it(trying to figure out why i saved it) and then I was mesmerized by Cherno's excellent slow-mo coffee making and I just decided to stick till the end and wow I learned something great today.
CZcams is a strange place
That intro was one of the most cleanest, beautiful things I have ever seen. Well done, man!
Sorry, I have a question. Why don't you use namespace std?
Yeah, and also, what programming ide is this?
Visual studio 2017
Technically he does use namespace std. He details this in the video. He does so by explicitly declaring it each and every time he makes a call from std. What he doesn't do is obfuscate the use of namespaces by calling "using namespace std;" which is essentially the lazy way of doing so and can easily make it difficult to remember which function is being called from which namespace.
Nice name tho...
Thanks a lot.
That intro was oddly mesmerizing
I was like: "GASP!" when he waved the mug around the laptop!
someone here has ptsd because of accidents with coffee. I understand you perfectly :p
@@dionyzus2909 My dad spilled a drink on his laptop keyboard. it was instantly dead.
Great job, I loved this video, it was very informative! I always try to follow best practices and I've been programming in C++ for about a month now. I am going to take your advice.
The same with me too. Very new to C++ (3 wks now). I like this very channel. Already learning a lot. A big thanks to @TheCherno for his great job.
wtf man the last time i watched you it was you talking over visual studio now you out here being a videography pro
Lol, love it... more and more of Peter McKinnon :) Keep it going!
Really good, interesting video. I've only recently started learning C++ so this was something I've never even thought about. But why on earth do you think people who've clicked on a programming video would want to watch 2 minutes of you making a coffee first??
I was waiting for this video from quite some time and now wait is over. Really good video !!!. Thanks
Thanks for the tip on the header. I haven’t experienced any issues because of that but I can see how that would be disastrous in a group setting
Is that a coffee or a milkshake?
mean mole It is a latte. Basically a fancy steamed coffee milkshake.
Hard to call it a milkshake when it's not cold or made with icecream. It's simply a cafe latte, espresso + steamed milk.
@@warrick_lo Very brave.
I know you probably know this but i just wanted to leave this out here because you can say "using std::(function name/ class name);" (ex. "using std::cout;"), i think this is better because you are just saying exactly what you want and it's easier because now if you want to for ex. to use another function from a namespace called "cout" you can actually see that there is already a "using std::cout;" so for this case i think that it is better plus you can actually see what functions/classes you are using from std(any namespace), again just becuase it's the best of both worlds dosen't mean you must use it, i just find it easier and better this way, and the rest is basically the same as Cherno, just wanted to say this, as he did not cover this.
These intros are the cherry on top of the cream on top of the cake. Beautiful content in every sense.
At least he knows the important of a good espresso before anything is said.
What I was always told was it ok to use in main and no where else but I think I might stop doing even that
I thought I accidentally clicked on video about coffee
Very nicely explained. And other than that video editing and intro section was also nice.
Thank You.
Thanks for the video! It’s very interesting.
I agree with you.
Could your please provide the IDE that your are using for c++ development? I am now using Vscode, but I see that your IDE provides more information when debuging a program. Thanks!
First two minutes of the video == using namespace std;
Skyclad lol.
I think the better question is why does EA have their own Standard library?
The std library is often not really fit to use in low level engine programming because you need performance, thread safety, memory leak safety etc. pp.
Creating your own std library that mimics the actual std library but is better for your purposes ist quite common actually.
they need to hide the code where they fleece the customers with dumb dlc
@@PoojaDeshpande84 The EASTL is publicly available on GitHub, and code that handles DLC wouldn't be defined there. I get that you're trying to knock EA here, but at least say something that makes sense.
Bc Std::vector is not very well optimised
this video starts with 2 minutes of making coffee
there are plenty of new programmer channels now, but yours is still my favourite number one, bc the others just brag about how great their life as software engineers is, while you offer valuable interesting content.
I'm newer to C++ and have thus far been using namespace std. I recognize the reasons why it isn't optimal to use it, however my rationale is that I want to get a hang of the syntax and function library before worrying about namespaces in general. It probably is about time to make the switch before my programs get too complex to keep track, though.
I wanted to add the addendum to this comment that I have since stopped using namespace std in my programs. It took about a week before it felt natural. For reference I'm usually coding about 30 minutes a day. It was worth switching, but I am glad I used it for my first few weeks of coding in C++. Stopping helped to develop a better understanding of scope, which becomes more important as your programs become more complex.
I am also new to C++ , and searching for people to join me on discord to build a small community to share knowledge and help newbies.
@@laveeshtomar I am interested in talking to other aspiring programmers. Feel free to post the discord link when it's up!
That is a good argument. Makes me think twice about using it.
That being said, it will be an extremely long time if ever before I need to worry about massive files. No more namespace.
Another great video. Never looked at it that way! Thank you!
So, is it ok to use using in my own library code's cpp file? Say if I have arraylist.h and it defines a class in namespace DataStructures, in arraylist.cpp can I use using namespace DataStructures to avoid putting it in front of every member definition? Is it a bad practice as well?
Hold on!
..I need to watch this with a coffee. Brb.
I really like having complete knowledge and control over my types. Yes in theory if I don't import any other stl like classes I will know what I am working with, but I want to see it explicitly, and I also want to see it explicitly in 6 months. I also don't use auto much.
I guess this also comes down to what you work on and your role in the team. I do a lot of reverse engineering and often think about actual individual bits of data. I am often also the guy that they call when something "should work but it doesn't" and it's always some small detail like a bug in a private method. Abstraction and short code are great, but some of us have to dive into the ugly details so making everything explicit became a habit of mine.
I thought it was going to be about slowing the program down to begin with given the extremely long intro
but what he actually said makes much more sense
I am watching this at morning and the intro... I didn't had a coffee
Well there it is, one of the examples what separates good coders with lousy ones.
Imagine not using namespace std
Good to see someone has basically the same view I do when it comes to name spaces.
I'd probably used vector as an example you have std::vector and then you can have math and graphic libraries with vectors which are entirely different in nature.
When I first stared using C++ the std wasn't near as good as it is now. It was pretty easy to write better performing code on your own. So people very often created their own versions of what was in std.
99.9% of the time I don't use it. The times I do it is usually something short that is being made and I probably just had the default code system create a console project and was to lazy to delete it to simply show someone else how something works. For large projects I never use it.
adorable intro and as always your explanation made it more easier to understand :) ; Enjoyed it ;
Whats the song used in the intro?
I've been programming in C++ since before namespaces existed . Using "using namespace std" was something I latched onto pretty quickly when namespaces first became a thing and it hasn't ever confused me before, nor has it ever confused anyone working with my code. I guess it's just a matter of style.
So my question is, when you are using functions from eastl, are you also using functions from std simultaneously? You mentioned that eastl is an API replacement for std, so I would expect one would only use one without the other.
that intro tho
GAGE was long.
The intro honestly adds so much to the video even though it has nothing to do with namespaces.
Thanks for the very good explanation. Should I use type alias in the header file? Unreal Engine 4 coding standard suggests these types...
using FString = std::string;
using FText = std::string;
using int32 = int;
Interesting. It's like the fact that I prefer to code in my native language (french) so I don't mix my functions (in french) and more general functions (in english).
Then there’s Rust.
#[macro_use] // I think that’s the syntax.
extern crate clap;
use clap::*; // oof.
Derp Marine i think if you have clap in your Cargo.toml then you only have to:
use clap::*;
You should post more these type of concept videos ,i was about to go to bed and after watching this well my sleep is been chased away .It was good and thank u
Very interesting, but I'm kind of curious, how often is it for programmers to use different namespaces? Would you happen to have a video on that?
6:36 i laughed so hard
I'm a simple man, I get a youtube notification when Cherno uploaded a new video, I hit like.
You are so simple, you can't even come up with an original comment.
good video, i dont know how to code in c/c++, but looks like this is usefull to figure out what dependences you really used and what you can strip out from your code, right?
Awesome. learned how to make coffee too.
Nice intro but WHY SO MUCH CREAM
because it's a latte
Myself and most professionals don't because of name conflicts
Is there a special reason for using reference by parameter when not going to change the value of that parameter? Like on apple::print() he passes text by reference, but he is not going to change it, he is just gonna cout. Should I always pass by reference even when not changing directly the values?
i agree with the you. let make habit to avoid usinge for (stadand namespace) . its ok for own namesapce. using namespace xyz (ok), :) ty. can you tell your wrist watch brand?
Using std:: actually makes your code cleaner
It's and it prevents sudden headaches!!
Depends you could use it in function context and make a rule that you never use it in global context
Hey man. There are folks whining about the intro. Don’t listen to them, they are brilliant because you’re not just a robot teaching us code. We want to know you as a person in ways you’ll want us to.
Heck, throw in some drone footage of yer homeland Australia, We’ll love it.😍
Soo.. thank you so much, you taught me a lot of good programming practices.
Cheers
People come here to learn C++. They find a high quality video and class FOR FREE and still complain about seconds of coffee making intro. Those shouldn't be here then.
Fantastic intro, mate!
Lovely intro
lovely topic
and best video
in the CZcams i have ever seen
Keep going..(:
What's the point of the long intro?
slap_my_hand porn
He probably bought an expensive dslr and doesn't do much with it so he needs to make long intros to justify getting the camera
It's art. Art is the point of art.
+slap_my_hand Coffee is like 80% of coding, Its a coding tuturial.
Whats the point on asking whats the point of the long intro?
using namespace std
at the top of the program violates the reason why namespaces were added in the language.
New to programming, and saw this video and thought if I don't declare namespace at the top, where am I supposed to declare it? is there a list I can refer to?
best tutorial on how to make coffee :) and nice cinematics of it.
people don't like opinions
Vim is better than Emacs
*tabs*
"using namespace std;"
ahh the mark of an amateur
Karan Lobana could not agree more
Heavily disagree. Because like any naming convention, whether “using namespace std;” makes code easier or harder to read depends entirely on your code.
Just like in actual language, btw. If you were to stick to your guts you’d use punctuation and capitalization. You don’t, because implying proper qualification of your sentences is enough to be understood. Just like implying proper qualification of functions is often enough for code to be understandable.
It’s like the mall grabbing of the coder world.
I agree its context dependent. If I were to be showcasing a piece of code on a slideshow, I would use this expression. But for any professional codebase or project, I would never use this. And most would agree with me on this.
Ah yes, the mark of code that will never see the light of day :)
Very well explained! Thanks for the tip about using a namespace!
Mate..what a great content! Thx for the explanation!
Hands down one of the most pointless intro I've ever seen.
Simon Dimas no need to be rude. You could express the same sentiment saying something like ”Thanks for the videos. That intro was a bit long and doesn’t really relate to the content. Keep up the gopd work!”
By the way, I liked it as a one off thing but it would get tiresome if it was in every video.
@@martinlarsson8947 Whip anf naenae
I hate these way too long intros
SteakieFrags same, seem pretty pointless for coding vids
Every time i see that i wanna click away from the video.
I think his other passion is videography, so be supportive ;-)
Toxicity Game Dev He would be able to create another channel for that.
Or you just don't complain about it and let him do what he wants with his skills / free time, I am sick of people saying "Im here for the this", if you don't like it. Go else where for you C++ videos.... Oh wait, they are all terrible so I guess you can either give him props or complain and watch it anyway lol. Because lets be honest, you are going to watch them anyway...
The coffee making part was so delightful
Watched half, didn't even need to finish, I was sold on not using namespace in like video three lol, so I was already convinced before this video. Great work Cherno.
This is 2021November, a bit late....but your coffee /latey making procedure is both calming and mesmerizing...good start for a vid!
I was wondering that same question every since I first watched your videos. Yeah, when I first found out "using namespace std" I really thought it was the best thing in the world. But your point is surprisingly compelling. Makes me think that if I ever intended to make a complex code library with more than a handful of classes I should actively try to understand and use namespaces to segregate classes.
I am laughing so hard on the intro
Welcome back to my Coffee++ Series, my name is Cherno
The idea was explained very well. Thank you :))
Why you don't mention using X=std::T;??? or typedef std::T X;??
It's really useful when you need using overly nested container such as: std::vector
Thanks for clearing the confusion
Hello I am new to C++ (From a different language) what did you use to create the GUI for your Hazel Game Engine
Do you have video about EASTL? And when to use it? Does it provide advantages (still today?) ?
Or use EASTL always when programming (cache-friendly?) Game or Game Engine ?
Hey, you know you can just using namespace std; and then you don't have to type std everytime
wow... imma still use it
i learned by using namespace std, how can i unlearn it? I don't know when i have to do std:: and when not to
0:10 roflol, is that your daily coffee ration? Reminds me so much of Seattle where the people drink coffee like trucks drink gas.
You're a better C++ teacher than any of my C++ professors.
You've convinced me! Excellent video.
Yeah, I don’t use it for pretty much the same reason:
- I want to know where the item is coming from.
can u please make a video explaining what is virtual memory and what is address spaces because that is quite confusing to me and I think I don't understand them well and thanks.