31 nooby C++ habits you need to ditch

preview_player
Показать описание
How many nooby C++ habits do you have?

Up your C++ skill by recognizing and ditching these nooby C++ habits. Post how many you fell for!

SUPPORT ME ⭐
---------------------------------------------------

Top patrons and donors: Jameson, Laura M, Dragos C, Vahnekie, John Martin, Casey G, Pieter G, Krisztian M, Mutual Information, Sigmanificient

BE ACTIVE IN MY COMMUNITY 😄
---------------------------------------------------

CHAPTERS
---------------------------------------------------
0:00 Intro
0:13 #1. using namespace std
0:42 #2. using std endl in a loop
0:55 #3. index based for when range-for fits better
1:10 #4. rewriting std algorithms
1:34 #5. using C array over std array
1:50 #6. any use of reinterpret cast
2:39 #7. casting away const
3:24 #8. not knowing map bracket inserts element
3:39 #9. ignoring const-correctness
3:59 #10. not knowing string literal lifetime
4:12 #11. not using structured bindings
4:47 #12. out-params instead of returning a struct
5:04 #13. not using constexpr
5:21 #14. forgetting to mark destructor virtual
6:10 #15. thinking class members init in order of init list
6:37 #16. not knowing about default vs value initialization
7:28 #17. MAGIC NUMBERS
7:41 #18. modifying a container while looping over it
8:30 #19. returning std move of a local
9:10 #20. thinking std move moves something
9:42 #21. thinking evaluation order is left to right
10:56 #22. unnecessary heap allocations
11:23 #23. not using unique ptr and shared ptr
12:11 #24. not using make unique and make shared
12:23 #25. any use of new and delete
12:52 #26. any manual resource management
13:24 #27. thinking raw pointers are bad
14:25 #28. using shared ptr when unique ptr would do
14:48 #29. thinking shared ptr is thread-safe
15:26 #30. mixing up const ptr vs ptr to const
16:00 #31. ignoring compiler warnings
Рекомендации по теме
Комментарии
Автор

I used to think I learned C++ in college. I realize now it was C with classes

jamesdowner
Автор

The main thing I took away from this video is that I have no idea how modern C++ works. Classic mCoding. Keep up the great work!

andrewglick
Автор

12:20 make_shared also allocates the resource and the control block together, making it much faster (and cache friendly) than directly constructing a shared pointer

yxlxfxf
Автор

An addition to the last comment about pointers coming from C libraries: Many C libraries that expect the caller to free the object often also provide their own "free" function for that specific object type. So the solution is to make your custom deleter function call that special "free" function. Also remember that the custom deleter function, which is passed as a template parameter to unique_ptr, can be a lambda too.

sledgex
Автор

I don't even know C++ but I'mma watch 🤣

FADHsquared
Автор

PLEASE DO NOT STOP MAKING THESE VIDEOS, youtube was really lacking these proper advanced(? at least for me) tutorials on programming.
I am learning to code for ~3 years now, and I have been looking too learn more advanced stuff multiple times, you are the holy grail.

kacperkwasny
Автор

This is really valuable content, thank you!

I think I managed to pin down what makes this really nice. This isn't necessarily the kind of advice that you'd get by reading a book (or at least not the books I read). I learned this kind of stuff mostly by "folklore", i.e. some more experienced developer told me to do things this way because it's neater. The value of this stuff is that you're making this advice accessible to everyone, not just those with senior mentors! :D

I really appreciate that

ciscoortega
Автор

I only use 1% of C++ I’m pretty sure 😂

astrahcat
Автор

I learned SO MUCH with this video. I started with python as my first language, but now I'm switching to C and C++ because I decided that I want to work with embedded systems. Also, I watch your videos since long time, keep what you're doing, your work is awesome.

errodememoria
Автор

I am a noob myself, but I think another common nooby habit that's worth mentioning is using push_back instead of emplace_back when putting a newly created object in a container right away

StealerSlain
Автор

Wow, this video is simply excellent, the tips you gave ranged from basic things to more advanced stuff and I'm thankful to you for learning new stuff I didn't really know or understand.

notproplayer
Автор

alt title: james calls me a noob in 31 ways

Orincaby
Автор

At last, a YouTube video on C++ done by someone who actually knows modern C++. Thank you, thank you, thank you.

But I'd like to humble suggest a number 32: declaring all your variables at the top of a function. This is a holdover from old-fashioned C, and I still see it taught in some places. Don't declare a variable until you can initialize it with a meaningful value. (BTW, even C hasn't required this coding style for about 20 years.)

rdwells
Автор

I just started C++ and you show me all the things not to do. It‘s like telepathy😅

chennebicken
Автор

This is really nice to know. I haven't coded in C++ for quite some time, but back then, even though C++11 was already out, we weren't allowed to use it in the bank I was working at. Almost a decade afterwards, I see that C++ has evolved quite a bit, and seems really nice!

alicewyan
Автор

Ive been programming on c++ alot lately and this tips are soooo helpfull, I do make alot of newbie mistakes so thank you for making this. Im waiting for a part 2!

JotaOcaranza
Автор

Great video! A lot of tutorials/documentation found online teach the "old" C++ and make C ++ appear less appealing to novices

tom_zanna
Автор

You should put the supported C++ version alongside it. As someone using C++ 11 in production for compatibility, a lot of these may be unavailable.

Frumpbeard
Автор

The point about "raw pointers aren't always bad" didn't use the best examples, because in both cases you could instead use references. That way you'd not have a raw pointer, *and* you also wouldn't have the unchecked dereferences in both functions.

There are situations where you might still want to use raw pointers, but that's where you're expecting null to be a possibility as well, which is only because you can't have std::optional<T&> (which some would argue is a bad decision to have made).

tobiasgehring
Автор

THANK YOU! for making it easy to remember about const and pointer thing, being a self-taught programmer with few years in C++, I found atleast 30-40% stuff that's new to me.

mukulkumar