Forbidden C++
Вставка
- Опубліковано 20 бер 2020
- The unimaginable evil of C++ is unleashed. Be afraid. Global Variables, Macros, Void*, Goto, Namespace STD, New & Delete, all the tools every C++ programmer needs... right?
Patreon: / javidx9
UA-cam: / javidx9
/ javidx9extra
Discord: / discord
Twitter: / javidx9
Twitch: / javidx9
GitHub: www.github.com/onelonecoder
Homepage: www.onelonecoder.com - Наука та технологія
Hello! During these miserable times, I hope this raises a few smiles. Anyway, take care of yourselves, and don't forget those that may need some additional help. Keep washing those hands!
I care about old processors and processor architecture
@@festiveFurry I do too. I just wouldn't like programming for their limitations.
Isnt the "dark side of c++" all of it?
It did raise a smile. Enjoyed your attempt at thespianism :)
I find it ironic that it takes 14 days to show symptoms and 14 days to recover...28 days later...
intro is clearly fake... everyone knows programmers don't go outside.
That was his basement.
@@alphaforce6998 How could he not know. Programmers are so wealthy, we buy hundreds of yards of land to create a huge basement that looks like its outside, but without other people.
@@Kirides "outside, but without other people"
Those are the best five words I've ever seen put together
He is a programmer, so he can afford to hire an actor
Can confirm. I haven't left my house since the Coronavirus started. Neither have I left it a month before.
"Sir, why are you walking around with a hoodie and a shovel in the middle of the night?"
"Uhhh umm uhhh just making a programming video"
I would be more worried about his better half's reaction to him digging up her plants 😵🥴🤬
Phil Boswell can you restate that in terms most people can understand?
@@engineerbot his spouse might take exception to his desecrating the vegetable patch.
bravo
he coulda said his toilet was bogged up …..
14:47 Way back when I first learned C++ (in college), I wrote an assignment with a goto for clean-up. My professor forced me to re-write it even though there was absolutely nothing wrong with my usage (he admitted so himself).
So, I re-wrote it using a recursive function. He hated that even more. :)
My school jumped from C++ to Java halfway through my time there, so many of my upper level classes used Java for everything.
I learned that my professor didn't actually check our homework by doing all my assignments for our Data Structures class in Clojure.
(Gotta say though, a different professor actually laughed and gave me an A for a Numeric Methods assignment I did in Fortran 77.)
Jeff Spaulding
I wrote some text parser in Cobol in my first year of university and both young teachers and colleagues thought I was some kind of wizard.
@@cezarcatalin1406 “Can read books/other resources and learn new stuff = wizard” - because so many people can’t learn stuff on their own, or have no drive to pursue anything “intellectually hardcore” outside their realm of experience. And also because so many students just do the minimum needed to get by - I swear that teaching sometimes must feel like being dumbed down by all those souls in the classroom that only want a grade and move on…
Oh, recursive functions? *Pulls out pepper spray*
@@thomaspearson3698 After you program in Scheme long enough, you start to feel that way about while loops.
Me opening my IDE at 3am without a dark theme 1:05
underrated comment
Made me laugh, got my like
True !!! 🤣🤣🤣
OHhohhoh shit i feel that right in my eyes !!!
cries in codeblocks
"...And within lies the most heinous and evil forms of C++ code ever known. Would you like to look inside?"
*Cut to my C++ code*
I was watching this video while coding and immediately moved my global variables into int main...
At a good time too, since it was right when the errors were gonna pop up.
@@username6338 he has various alternative options in no particular order being
Unity
Unreal engine
Lumberyard
Ogre
Game Maker Studio 2.3
Construct 3
Gamesalad
Clickteam Fusion 2.5
Buildbox
001 game creator
RPG Maker
Dreams PlayStation 4
Scratch
Stencil
Visual Basic
@@username6338 technically those programs can also create Apps and software
I’m no longer a programmer, merely a data monkey, but my years as a programmer were perfect training for lockdown. In fact, I’ve quite enjoyed it.
felt that...
who else expected one of their github repos to come out of the box XD
Lol
You got me :d
An experienced user that joined 12 years ago and has no content, goddamit why wouldn't you.
I love this comment so damn much because i relate
😂
I appreciate this video a lot. Because it doesn't just parrot "Don't EVER do these things" but actually gives examples why, but also gives a case or to where they actually make sense.
I agree, I hate when there are youtubers who say "never do {this}" when there is always a time when it's justified.
Global variables are a sin and programmers who use it burn in hell
@@typowyjnn2300 they are more of a problem in JIT languages rather than compiled ones.
@@typowyjnn2300 Embedded?
@@typowyjnn2300 I use them fairly regularly in game dev, you have to have some kind of global state, and global variables are the easiest and simplest way of doing that. You can also use classes to make it so they cannot be modified without using the methods of the class itself, meaning you don't have the problem of global state being able to be modified at random. I also use a "glbl" namespace so I don't pollute the global scope. (and thus local scope as well)
“Processors from the 80s, no one is going to be interested in that surely?”
*cries in embedded hardware*
Felt that deeply
... on a Z80
Truly a blow as low as the hardware's processing speed.
Programming on a TI-84 belike
Cries in fossilised hardware
#define true (rand() % 100 < 98)
This is just pure evil
Quantic physics ;)
Interesting to note, that if you initializing random generator with a static value, you would be getting consistent behaviour with that define. Wild, but consistent.
Don't forget
srand(time(NULL)) this will make it more evil
Володимир Герасимчук No you don't initialize it like if you would do variable = (rand()%100
@@whythosenames srand(42); // no, I DO initilize it like this
Probably the greatest intro I'll ever see for a coding video
Thanks Usman, I like doing the sillier stuff XD
javidx9 That's why I love this channel and you are one of the few coding channels I'm subscribed to :)
javidx9 yes your intro is deemed kek. expertly done.
I completely agree!
you should see cs50 lectures intro
2:07 Global Variables
8:11 Macros
12:40 Goto
16:27 (void *)
22:23 using namespace std
27:31 new & delete
Me, who's only been coding in c++ for 2 months now: "I like your funny words coding man"
Bruh, I don't even know CPP.
;(
I think his main problem is that he hasn't numbered the lines.
Like, yanno -
10
20
30
etc
This comment made my day😂👌🏼
Me who knows Rust: *elitist smirk** _look what they need to mimic a fraction of our power_
Best macro ever:
#define private public
even better: #define public private
@@djordjermus4082 or why can't i access this function, is it clearly public?
cease, demon
This is actually very handy at the right times
#define protect delete
Now all your variables are safe
Timestamps:
2:00 Global variables
8:00 Macros
12:36 Goto
16:20 Void *
22:17 Using namespace std
27:19 New and Delete
thanks you saved me from wasting 33 minutes
@@1889990 "Wasting" That's a funny statement. Watching this video through to the end without skipping will benefit YOU significantly.
@@Drqonic It wont benefit that person.
He's got a busy life....
Idk, just assuming... whatever...
@@The-Cat and there are 24 hours in a full day. 33 minutes won't take shit from him.
@かわいいreo Not really. 33 minutes is nothing.
TL;DW: The C++ community's failing attempts to erase the C from C++.
just start from scratch lmao
Why Scratch? Can you even call that a programming language?
@k je Au contraire.
#include
#include
int main(void)
{
int c = 'c';
int cplusplus = c++;
if (c == cplusplus) {
puts("See? I told ya!");
return 0;
} else {
puts("Clearly this compiler is broken);
abort();
}
}
@k je what are you talking about? That was C++ I wrote. It just happens to be perfectly valid C by coincidence.
@@sodiboo I can't believe this guy is serious
"The most heinous and evil forms of C++ ever known"
So, like...literally any production C++ for a video game?
This comment reminds me of the fast inverse square root
Whats wrong with games and c++
@@metagen77 Well... I don't know either.
@@billgatest7563 That was done back in the 90s. In modern times leave the thinking to the compiler and just use sqrt() and others, it'll handle it much better than most of the programmers.
el que explique no es gey
the c++ dark side "box" is actually very bright when opened
🤔?Coincidence?🤔, I Think Not
White IDE
it's called the dark side because once you gaze into the box, everything will become dark
There should have been red light like in Hell :))
its all made in light mode IDE
2 minutes in and i just want a whole series like this.
lol, If im kept at home any longer, that may happen XD
@@javidx9 that would be perfect
Yes, please!
here here
@@javidx9 well the lockdown is here sooo
"Eventually I will consume all of the memory in my system" - Wait, it isn't the goal?
You're thinking of Java.
@@petergamache5368 Java is just easiest of them all to get there xD
100% memory speedrun
@@patrickgono6043 Now that sounds like a worthy challenge!
You can also put your "using namespace" inside a function I believe, which is even better than leaving it outside in the global space, because it goes out of context when the function ends rather than when the file ends... =D
My takeaway from the beginning of this is that the briefcase in Pulp Fiction actually contained a void pointer.
As a programmer who is quite inexperienced with C++, this was an extremely informative and easy-to-understand video. I'll definitely be coming back to your channel in the future. Thank you. :)
@Marcelo Rocha Any more you may know of ? Please put them here :)
Oh my. There's a lot of weird c legacy in cpp. And all of these 'evil' tools are still tools after all. One just should use them correctly. This is the most difficult thing in programming. I remember my first year - I came across all these and the situations where they are a pain and the ones where they are the best
As a programmer who is quite inexperienced with C++, this was an extremely informative and easy-to-understand video. I'll definitely be coming back to your channel in the future. Thank you. :)
Void* brings up bad memories for me. In one of my classes we had to finish programs to implement various algorithms, but the person who wrote the pre-written parts loved to use void*, even when the type was very clear and essentially always the same. This lead to all kinds of casting problems.
void* was typically used in C programs because they lacked OO features. C++ introduced better ways to do the same.
This seems very interesting, you actually cover the bad and good usecases for methods as well, I love that.
I actually laughed at the redefinition of "true" to a probability of being true. Nice video!!
+1
That was the first time in this video that I actually said out loud 'now *that* is evil'!
I assume this... gift will work on anyone who decides to blindly trust and include your code into their program?
This could result in a catastrophic hair loss
@@MrCh0o seems like an awfully evil way to enforce good habits, I love it
@@fabricatorzayac lmao
1:23 is where the iconic "Hello" strikes.
iconic eh? XD
Mohammad E. It’s just what I thought. His “Hello” is so iconic. xD
Thanks, thats what im here for
22:05 "I'm impressed, well done you've made this far, not many people have a stomach like you" me with a confused face watching the video understanding like only 20% of those cursed techniques
define int float; define true (98% of the times) lmao I died laughing so hard
Bjarne is clear: There are only two kinds of languages: the ones people complain about and the ones nobody uses.
Ritchie > Bjarne
Ritchie and Bjarne sat at the same lunchtable for 16 years or so (Bjarne said in some interview), and Bjarne is a big contributor to C and thinks C is a great language (why else would he take it as a basis). While this is no argument against the '>', it would indicate they probably have very similar views on things.
@@ccgb92 troll
@@sea-ferring Ritchie > Bjarne easily. If it wasn't for Ritchie's C, C++ wouldn't even exist.
I wonder if Ritchie's keyboard is broken. Why he used * for pointers instead of @ just like in Assembly? It's much more intuitive to use @.
1:20 POV: You are just regaining consciousness after having been kidnapped by javidx9 and being taken to his dungeon
This is one of the best C++ video tutorials I've ever seen.
Clear and accessible presentation, special effects and artistic dramatization. Absolutely super! Thank you!
Remember in my first year of uni I was learning C++, my first proper programming language, and for my first assignment I filled my program with goto's nearly everywhere where I could have used a while loop. 1 week before submission I showed my progress to my teacher and he ripped me a new one. I learned quick that day, a goto is a no go xD.
The assembly produced would be almost identical. The sanity of the next person to touch your code... not so much.
What they never teach you in any college is that the stuff you are taught won't translate to marketable skills. People who can't learn programming on their own just by doing it with self-motivation will likely end up doing customer service or tech support... but hey, look at that degree. Makes them feel special!
@@alphaforce6998 I'm learning c in my first year of uni, the exams are in a month, and I still don't know how to properly program in c, I have finished watching several toturials and still I can't seem to grasp it, what I'm I doing wrong?
@@magnuscoles5010 if you want to learn c++ have a look at The Cherno's UA-cam channel. He has a whole playlist of great c++ tutorials
@@alphaforce6998 idk I learned C++ from the ground up all on my own and it hasn't helped me land a job...the reasons are all self inflicted like a lack of confidence to apply places, worry that i don't have the qualifications needed, etc...so a degree is the confidence boost some individuals need, not to mention it helps you make connections to get your foot in the door somewhere.
The Halloween special we never knew we wanted..
MindSpyke we surely did need one though.
Exactly!
Hello there.
After years of programming, I think this video made me finally fully understand the static tag. Your explanations are really clear and concise
Professional software engineer with 20+ years experience here (a good portion of that in C++).
As others have already said, this is an excellent video for any C++ developer of any experience level. I especially like how you show both problematic and acceptable uses (or alternatives) for each of these techniques.
I did feel you made ONE MAJOR OMISSION in your discussion of new and delete, and I mention it because you have yourself such a great opportunity discuss it, before side-stepping the issue, and jumping straight to the “correct” solution. I really wish you would have covered how exceptions interact with new and delete, because that is where I see many developers get into trouble. The more obvious case would be where a thrown exception prevents a delete from being called, would have been a great place to start. The more subtle situation (and the primary reason for make_unique() to exist) is an exception thrown within a constructor. These leaks can be particularly tricky to track down.
Again, thanks for the great video.
You explained dynamic memory and the use of "new" and "delete" in a few seconds than an hour and a half Comp sci lecture. 😮
std::audio developer: Making an audio adapter work as expected is very difficult bla bla bla.
game programmer: just give me this and everyone will be happy: std::audio_source::play();
Error: expected a 'better', instead you typed 'in'.
@@EmilParkour I think your compiler has a bug. That's not where the error is located.
"You don't own your memory, the operating system does."
Well,. the receipt I got here from the computer shop tells me otherwise, SO I"M NOT GONNA DELETE.
i don't think that's completely fair. without the CS lecture, this probably wouldn't be so easy to you
2 minutes in and it's already the best thing I've seen all week.
Thanks Michael!
@@javidx9 my friend, thank you. I've been programming for 20+ years and watch every one of your videos. They are all fantastic and I have suggested them to anyone that mentions to me that they're interested in learning programming. I think the knowledge you're providing is enough to change life trajectories for people out there if they're looking to learn and better themselves.
system("pause"); you should talk about exploitation some day, i really want to hear you talking about such a beautiful topic
i use that all the time...
@@numbdigger9552 Here is why you shouldn't:
Imagine someone making a program called "pause" (which, apart from just pausing, also executes some malicious code) and put it in the same directory as your program. Or even worse, in any directory that is in the path environment variable.
In other words, when you use system(some_program) you have no control over what some_program does, and what it does could be different on different systems.
Obviously, when you're just writing an example program for yourself, there is no harm in it, you're executing it on a system where you have control over any program, including pause. Just keep in mind that in production code, this can create security vulnerabilities.
@ well since i have no plans in publishing anything, it is not a concern
@ If you had access to a machine where you could replace pause in such a way then your program is already running on a compromised system. What would stop someone replacing a shared DLL you app uses ? You wouldn't need system("pause"); for trigger for that.
After years of dicking around with Python with an inane fear of C++ I have started to delve into C++ and my god am I learning so much from this channel!
I didn't know about goto, seems useful.
XD What have I done??!!
@@javidx9
You've opened the gates of bug hell. XD
The Linux kernel runs off of goto
@TBit - Nope, use a
do { ... } while (false);
and in the 'guts' (...) use break; to drop out of the loop when you need to. As it is false, it will never loop more than once.
Behold the "DDW" (Doug's Do-while)
(never mind the name... long story)
Cheers,
goto is from "c"
12:38 Ah yes,Goto, I'm glad I stopped using it early when I started learning to code, thanks of course to all those C++ Discord users who would completely neglect the problem I was asking about and start pestering me about why I used a Goto as opposed to a while/for loop while simultaneously spamming pepelaugh emotes all over the place, classic!
They were right though, even though I was still writing simple programs at the time, simply replacing Goto with some loop did solve the problem in some cases,so to any C++ Discord users present here, you guys are dicks, but also thank you!
Yeah, a GOTO loop is the same as a while loop, but the while loop is easier to recognize due to the encapsulation and naming.
It's not like GOTO is necessarily bad, but it does introduce unnecessary complexity. I actually quite like GOTO, but it should be used carefully.
goto's are never strictly necessary. Usually they're a sign of very sloppy thinking.
I've used them, rarely, in some driver code where the alternative would have been far less readable. But only EVER forwards to simplify complex logic ending in common wrap-up code. The backwards goto, on the other hand, is truly evil and I've never seen a valid case for it.
Any time you feel the need to use a goto... you should wrestle with the decision. If it doesn't cause you, at the very least, a sleepless night or a re-factor... then you've really not thought it through properly ; )
Using goto is something you need to agonise over and try to make peace with.
@@garychap8384 there *might* be the fringe case in working with a system that has VERY little RAM to work with (such as an Arduino), but I'm not a good enough programmer that I can use GOTOs any better then the compiler would.
Funnily enough, I didn't even know GOTOs were supported by C++. I assume it's a holdover from C because object oriented languages were still relatively new at the time C was developed. It gave me a good chuckle, and reminded me of my Assembly class in college.
@@Bistai949 Yeah I sometimes forget how old C/C++ actually is.
Great work, Javid! Nice clear explanations, concise, well-spoken.
Standard namespace was taught to us week one in my Intro to Programming I class. I changed majors after two semesters so I had no idea it was so contentious.
std namespace is obviously great. The "using namespace std" statement is the evil thing.
When my professor told us that we should never use global variables, someone asked why. He said that global variables are evil and didn’t explain any further. Thank you for sharing the dark bits of C++ and explaining them, I finally know what not to do
Sounds like a bad/lazy teacher and you probably paid too much to have him not teach you anything.
Because it's harder to test is one good reason. Units needs to be isolated to behave predictably. Applies in any programming language. He should have given reasons like this however.
If your professor couldn't or wouldn't tell you why, they shouldn't be teaching.
I remember my professor starded explaining when a friend of him wich liked to use global variables fked up a whole program (it was a big project imo, like scientific research) for a redefinition of a global variable on some file and they had to debug for 3 days and lost a huge time and money due to that e evil behavour
Isn’t it clear, though? Imagine I had a light switch in my bathroom that controlled the light in your bathroom. That’s the first thing that should come to mind when you look at the concept of a global variable! Good luck figuring out the switch is in my house :) Of course today we have IDEs that let you find all uses of a variable at a press of a key combo, but you also need to be paranoid enough to think of inspecting that variable. And any “protocols” people agree to with respect to globals always break down.
8:55 The double-evaluation issue aside, that macro declaration is also missing parentheses around each occurrence of a parameter in the macro body :P
Yes! I thought he was going to mention it. This can really screw you over when using macros.
You can do some really evil things with arithmetic operators in those macros and just completely jumble up the order of operations.
yeah. i thought it was going to be an operator precedence issue
void* pointers are great for dealing with unknown data, like when you need to write data to TCP sockets.
I think that is what my MUD used them for since that was all TCP based.
void* actually provides some safety because it is explicitly a pointer to unknown type. You can’t increment/decrement/subtract/add/index/dereference until you cast it to another type.
Indeed. He himself gives an example with the function he wrote. A function that writes bytes to a file, it only cares where the bytes start and how many of them there are. The function will never interact with the data so it doesn't need to ask for its type.
@@azaria_phd Although there is danger in requiring the caller to pass in the size correctly and trust they don't cause an overflow. If you knew the type you'd also inherently know the size, I'd definitely use a template in that example.
@@abstractspaces8186 Using a template would result in duplicate code for every type you use it for. What you'd really want to do is use both: a template that does nothing but call the function that takes a void* and pass the correct size.
I stopped programming in C++ almost a decade ago. Since I'm stuck in self quarantine until god knows when, I may as well pick up programming again and have fun. This was a great video, and it made me remember a lot of C++ I learned back in the day.
Woe to you, on Earth and Sea. From the devil sends the beast with wrath, because he knows you will use global variables.
Me while watching the video: Err, I don't think _inline_ works that way.
You: The meaning of inline has changed over the years...
Me: better double check before embarrassing myself with a snarky comment.
C++17: Make Inline Great Again
Me: Neato! I learned something new today!
After seeing how you use the goddamn bit shift operator to write to stdout I thought I'd stop being surprised by how C++ mangles its own syntax, but here we are!
@@shukterhousejive Same. C++ is probably the most bizarre language evolution I've seen.
@@kneesnap1041 more like de-volution
@@tetsuoiiii lmao I'm with you there.
@Michael Deloatch Yeah, it's a great change. I'm not sure why others are using my post to hate on C++, it keeps getting better and better.
Absolutely marvellous! I love the humorous approach to what are very real issues.
Love your videos man! I think you are the best youtube programming channel. You actually provide useful information and tell it how it is.
Him: "I commend you for making it this far."
Me, not fully understanding it: "Thanks."
I got a _"Tales from the Crypt"_ jive there at the end and at times throughout.
This could totally become a new series worth accumulating some "horror stories" for sharing over time.
As always, thanks for sharing!
this "dark side" of C++ is so unholy, that not even 2 crosses could contain it
Very cheeky to see this in my recommended a few days before Halloween. Very cool video though and has so much good production for a video any old youtuber would have just made a run of the mill top ten list with minimal detail. I approve!
I think you've got another hit series on your hands. Can totally extend this to other languages as well.
"It's quite a modern one:" *pause video, try to guess*
"New & Delete"
XD
Modern... is ancient computer history still deemed modern ?
I mean, phenom processors aren’t considered “modern”.
Favourite programming video right here, thanks a lot. Also got me in the Halloween mood that is quickly approaching!
Thank you for the suggestions. I will try to use these techniques in all my code going forward!
That's one hell of an intro
That epic, scary... (Also well done) intro. That lead into... the... "Oh, Hello!" XD
spoiler alert
@@sonnenhafen5499 oh, sorry :(
@@kevinhansson2177 no problem haha, but this was really inavoidable since yours was the top comment at the time and the only thing visible as comment preview in the app xD i really needed to get this off my heart
@@sonnenhafen5499 LMAO XD That's acctually kinda funny. Still sorry btw :)
1:04 The dark side of C++ was surprisingly bright!
However, this video was extremely helpful for someone rather new to the language (and programming in general). I didn't know about the "any" cast, as well as the macros (#define). And I finally understood what namespaces are and how/why they're used. I haven't had the need to use global variables yet (outside my "main.cpp"), as my projects are still quite small, and are more on the "experimentation" stage, but I'm certain that it will be helpful in the future.
I've actually been using the void* to research how variables are saved into memory, by reading byte by byte the address of the variable through its size. However, this video gave me insight on how to do what I'm trying to achieve (and the reason I've been trying to figure out how are different variable types saved in memory)
"This type here makes no sense at all"
[Quake inverse square root flashbacks commence]
Evil bit hack
@@user-sl7ie9te5r What the f***?
Templates would also be a good alternative to void* in that example.
eww
Depends. Once I did a vector that could receive any variable. For instance: the first element could be an int, and the second one a double. I'm almost sure that you can't do something like that with Templates.
@@deepdark8192 Vectors are implemented with templates.
@@syfp4769 I did my own vector. From scratch. I don't think you can receive different variable types in the same vector when you use the std::vector.
@@deepdark8192 I don't know the context you wanted that for, but that sounds like it belongs in the box of forbidden C++.
I think a cleaner solution would be defining a union of the values/types you might want, and making a std::vector of that union.
10 print "This is a test"
20 goto 10
I believe this was the 1st program I wrote.
You had to do a mkdir x and chdir x in between to reach the fat16 directory nest limit.
Pretty sure it was gw basic
@@frankstern5270
To me too but it does not run though.. 😷
HELLO WORLD!
HELLO WORLD!
HELLO WORLD!
HELLO WORLD!
HELLO WORLD!
HELLO WORLD!
HELLO WORLD!
Honestly. Having started out using basic on one of those old "kids computers" that they had in the 90s that could run basic... I pulled a lot of aggro from my college C++ professor for using gotos here and there.
This is just such good content. You have truly changed my way of looking at progamming. Thank you.
The voiceover at the beginning of this was downright impressive!!
Thanks for the video! It was fun watching and you made it really entertaining! :) I specifically liked the "goto" use-case. I personally never had the need to use it by now, but I think there is a good reason why they kept it in the language. Although many programmers tend to have really extrem negative opinions about it, behind the scene when everything is compiled and translated into assembly, there are goto/jump instructions in our program all over the place.
Thats right Gilad, to the CPU & Memory, everything is global, and the JUMP is everywhere in programs... Its just us strange humans that require limitations in order to understand things XD
@@javidx9 It reminds me some video about genetic algorithms. There was simulation in which 100 objects roam some field, eat "food" and attack each other to survive. And after certain time top 10 of them provide code for next generation (with slight random gene mutations). Genes of the objects were program instructions by which they "live" (32 or 64 operators, I don't remember).
First generation gene programs of the objects were random. And after thousands of generations if you look at those programs all you can see is enirely undecipherable goto-gibberish. But this objects moved to the "food" in quite short paths, attacked other weak objects and survived till end of the simulation. Just based on their strange unreadable gene programs.
Makes you think about how our brains work.
This is great! As someone who writes firmware in C++, this is always great to sharpen my skills. I'm primarily an electronics engineer though....most electronics need good code to run!
darn right they do. Part of what made Ardunio into Arduino was the firmware that made it less daunting.
loved this whole video but i especially love that little comment you have about system("pause"). When i took my first c++ course, our instructor did not explain at first that we would need this. This was the first language I ever used so Imagine the dejection i felt when i couldn't even get a hello world program to run properly (as far as I knew) because I didn't have system(pause)
This is really cool, as a student, I've been taught a lot of the older ways of programming by older professors. I didn't even know of the newer ways of handling memory allocation. I've just been using the new and delete.
The criticism for why global variables are bad is not inherent to global variables and only really a concern for beginners. Yes global state has a lot issues but the concept is not inherently bad and extremely useful in many problem domains (e.g. games are a good example since there is a lot of shared state). Some people may prefer passing the global state around everywhere, but sometimes that is much more cumbersome than before. It may be wise to tell a beginner to C++ (or C) to try to not to use global variables if possible, but they should not be forbidden.
Macros are not a replacement for templates, and macros are fine when you are trying to add functionality which templates cannot do. C-style macros are unhygienic and can cause issues because of this, but they are not inherently bad. For beginners, they should be used with a lot of caution and the user of them must know the issues with them. This wisdom comes with experience.
The statement that "`goto` will, not might, cause spaghetti code" is only true for beginners. There are perfectly valid use cases for them in C++ which produces _cleaner_ code than not using them. This is because of C++'s lack of structured features, such as named scopes for the ability to break out of parent control structures or defer/scope-exit (the latter of which can be implemented quite easily).
`void *` is fine, and the complaint main is that `void` is not typed, which is the entire point of `void`. `std::any` is pure evil in C++ (because C++'s RTTI is dreadful). The example to replace `void *` with `std::any` is actually much more bug prone than before because if a pointer is passed rather than the value, the value of the pointer will be written rather than the value that the pointer is pointing to. And that use of `std::any` is incorrect.
I completely agree that `using namespace std;` is evil and I would actually go further and say `using namespace` is evil in C++ always. P.S. If people like C++ style namespaces, they should keep to one level deep any way because heavily nested namespaces do not aid in readability, refactoring, organisation, and more.
`new` and `delete` are bad but not for the reasons stated, which is just a complaint about _manual memory management_. (P.S. Placement `new` is good if you like constructors). `std::unique_ptr` is not a replacement for `new` with automatic memory management; brings along a new concept called "ownership/move semantics". They are __not__ the same as reference counted pointers, nor an alternative. C++ is not meant to be an automatic memory managed language, please don't try to treat it like one. This is does not mean you should try to use loads of small and manual memory allocations but rather keep the allocations to a subsystem, and let that handle it if necessary.
Ad global variables: If you have a single threaded application then you can, with care and a lot of shifting around of that gray mass in your head anticipate in what state a specific variable is at any given time. In debugging you can stop and then backtrack how you got there. But once you start writing multithreaded code all bets are off. Using global variables in concurrent code is just suicide.
Ad macros: Macros are a two-sided sword. On the one hand they can be very powerfull when used correctly. At the same time that is the reason why they are dangerous. As long as you write programs for yourself and keep track of what you are doing ... more power to you. But beginning with a certain size of project those macros tend to throw off people if they have to maintain the code somewhere down the line.
Ad goto: What you are saying kind of echoes what has been said in the video. There are SOME edge-cases where goto might make sense. But only then. If you start to code C++ with gotos like you code good old BASIC-code then you will run into spaghetti code.
Ad void* : The usage of void* is, IMHO, nowadays realy limited. Or at least it should be. The problem is that it is sometimes used in ways that are ingenious when first implemented but nigh impossible for anyone else to maintain afterwards. I agree that std::any isn't the be all end all and that you have to be just as carefull. However, pointer to pointer logic is IMHO slowly disappearing in favor for structured types.
Ad namespaces: There are instances where additional levels of namespaces make sense. It kind of depends on the size of the project. But I have to agree that it also tends to get out of hand pretty quickly, and suddenly you have a three or four layer deep namespace.
Ad memory management: Not much to add there, except maybe: He's using MS C++, and that actually CAN be written with both manual and automatic memory management. And again - concurrent programming and memory management are a whole different can of worms.
I'd like to say it all boils down to preference, but that is not quite true. It realy depends on whether the company you write code for has coding guidelines to which one has to adhere, be it as a freelancer or as an employee. Even in large OSS-projects you usually can't do whatever you want - at least if it is managed properly. If there is a code review and those guidlines are in place then you need a realy good reason why you should be allowed to circumvent them. And then you need to document it. It might look like harassment, but it usually is something needed to keep the code maintainable.
I speak from experience.
The team I'm part of is currently tasked with reimplementing old legacy code. The oldest programs are 20 years old, the newest about 5. It's a hodgepodge of styles, of techniques, of proficiency in the language, and nothing is documented. At first we thought we might be able to salvage at least parts of the code, but alas, it 'grew organicaly', meaning that one functionality got expanded by plopping a wrapper around it and then doing semi-rand stuff with it. There is no spearation between data-, business- and presentation-layer. The underlying database breaks every form of normalization in every conceivable way, the primary keys are all over the place, the naming of ... anything realy ... is inconsistent and half of the stored procedures are using indices instead of names which leads to some ... intresting issues. On the other hand we have SQL-queries within what could be considered the presentation layer if you realy want to strain you imagination. It is pure chaos. No documentation. Only very, very sparse lines of comments that usually do not help at all with trying to explain what was inteded to happen in wherever they are found. But on the other hand there are methods that are thousands of lines long. And somtimes there are pages upon pages of code that had been commented out. Variable names are random and more often than not have nothing to do with whatever value is stored in them. Hell, we have an application where the buttons are called "Button1" to "ButtonXX", the methods are literally called "function_1(int a, string b, SomeClass* c)" to "function_XX(char a, int b, void* c)", the variables are really called "a, b, c, d ... aa, ab, ac ...", half of them are literally names. Like "Jake", "Thomas", or "Martin". I'm not joking. I whish I were. The source code repository is contaminated with dead files, dead data, dead cache data, random shit and a boatload of features and images no one ever uses anywhere. Checking out the full repository takes an eternety as there are 250.000 files checked in - most of which are trash. At first we thought we might be able to untangle that mess, but we pretty soon gave up on that idea and started to re-implement everything. Every now and then we still have to put on our gloves and dig through that pile of dung - when we have to apply absolutely necessary bugfixes or when we are not 100% sure on how a specific feature had been implemented. It's always something that takes way too long for what is gained from it.
The reason it got so bad was because the ones who came before us had free reign, they were spitting out features fast and everybody did whatever the f'k he wanted. Mind you, some of them were seasoned developers, but that didn't stop them from creating that monstrosity. Now every one of them has moved on to greener pastures, and we are left with their legacy. When I came on board they had been gone for over a year and my current teamleader had put the current guidlines in place. Yeah, we are not as fast when we start a project, as we need to set up a lot of shit for later, but once everything had been set up changes can be made rapidly, bugfixes usually take half an hour at most, the code is reviewed so everyone is on the same page and the guidelines are upheld and we document our code.
@@brkr78 I have used global variables with threads is easy if you can make them std::atomic. But I still would prefer not to use globals unless it is clearly the easiest solution for problem. But even in this case it is likely global variable makes more sense as static member variable because it is often related to the class implementation. But multithreaded globals are quite a lot more restricted than single threaded globals because order of execution is chaotic in many cases.
std::any does not use RTTI. It actually uses a very clever type-erasure trick: It instantiates a function template for the type that you construct it with, then stores a function pointer to that instantiation. That function can be invoked to get information it needs about the stored type, like how to cast it back.
@@kered13 That is a form of RTTI. It might not be the same as the "normal" C++ RTTI with dynamic_cast, but it is still RTTI. A "clever" "trick" but insane.
@@GingerGames It works with RTTI disabled, it doesn't use the RTTI and any of it's drawbacks at all. In fact the only drawback it has is requiring a dynamic memory allocation to store the actual object. It's hardly "insane".
void* is clearly something which was very useful in C where you wouldn't have templates. Like for example, implementing collections without templates implies using void pointers, but that would lead you to typing the collection in the name of the variable in a comment or somewhere to have to cope with the lack of typing in your collection. It's a kind of nice and bad feature at the same time. I didn't know about any type though, that's a great functionnality!
This looked like sooooo much fun to make! I'm getting some "The Mighty Boosh" vibes from this too!
A classic example for namespace clashes on linux is when you try to include and . Both headers contain a function named "bind" and if you use "using namespace std;" you won't be able to compile the program.
I'm a newbie in programming (I picked C++ to learn how to code because of my masochistic tendencies) and at least I understood exactly why I shouldn't (for now) do the things you mentioned and not just "aim for best practices/build good habits". You explain everything clearly without being tiring, I understand everything even though English is my second language.
Thanks and that's encouraging to hear Drop Down, Good luck with your study!
well, i think it's very important to follow an updated tutorial on c++ because it will be extremely damaging to follow an outdated one. cause C++ has changed A LOT
@@eyadhisham8094 I follow the lesson schedule on a site called learncpp dot com, it looks updated but I can't really tell since I'm a newbie. If you have any updated book I could look use I would be grateful, I prefer having a book in front of me while at the same time compiling code on my PC rather than following video tutorials, which most of them don't go in detail.
@@jimmyking92 It's just an online reference site and I only ever learned C, but en.cppreference.com seems to be a pretty exhaustive source on various language features while also mentioning language versions that introduced them.
It does provide a nice level of detail for everything C-related, but I can't vouch for C++ side myself. The best thing is that you can find info on just the feature you're interested in without having to sift through a load of extra info. Hope you may find it useful.
@@MrCh0o Thanks for your insight.
javidx9: "I hope this hasn't given you any nightmares"
also javidx9: *the intro*
I like the change of pace, I see you changing up your production style javid, sick video!
For the goto-netsted-loop example, I would add, that you can solve this use-case in a high-level language by creating a function and returning from inside the 3 loops.
It may require more overhead passing the neccessary variables to the loop, but it makes it clear what is needed and you also typically have already a name for it like "findThingInArray" which is already a pretty good indicator for a possible function
"I've never experienced a problem with this approach myself."
Well then, it must be evil!
"Join me as we visit the C++ dark side" - Forgot to bring sunglasses against blinding light
You lie there you have a one!
Working @ home I had a good laugh at this, then shared it with my collegues... I bet some can learn something from it :D
The part about goto was great for me. Those little use cases make so much sense. I grew up programming BASIC (which meant spaghetti code out of necessity a lot of the time), and when I transitioned to modern languages as an adult, I read so many times that goto is dumb, don't use it (and of course, I understand why that's generally good advice). So I'd probably have used breaks or whatnot to get out of the loop you showed. But when you think about what the code looks like when compiled, all you really need there is a jump instruction that gets you out of the outer loop, so the goto is perfect. And I would argue it's probably more readable for the use case you showed. Will not be afraid of using it anymore when it makes sense.
It's stuff like this where Stroustrup's personality actually shines through. His character is somewhat woven into the language, and through this, he will live as long as C++ and its descendants are around.
Thanks for the flashback!
After college (1984), my first 10 years were spent on embedded systems
in which we wrote assembler and did not have any underlying Operating System.
I thought in terms of hardware and memory.
I somehow managed to sustain employment in C++, but I never felt comfortable
with it, and now....I repair small engines.
Abandoned the software engineering career with glee!
My Comp Sci professor taught us to use using namespace std, even with multiple files and headers, and never told us about the determent's. I had always heard it was bad practice but never understood why until this video. Very useful information
27:05 I want to add that you can also do things like this:
using std::cout;
using std::endl;
wherever you need to add a few couts for debugging or just normal program output without using the whole namespace and without making your code look like a giant rectangle of characters.
Ahh GOTO. I remember that back in my Windows Batch scripting days.
Yep, (6 years ago) in highschool, what a time for me. :) When you had nothing better to do in your shitty ass computer-based classes. Bypassing all their stupid registry restrictions and such. Batch scripts were the way to go.
@@Those2menoverthere
First you're dabbling in VBS then you're doing full on batch.
God, me too. I cringe at the thought of the spaghetti my code must have been.
For the last 6 years I have been working exclusively in java and I always miss some "evil" from c++. One of the "evils" I miss most is multiple inheritance.
Each tool can be both good and bad, depending on how it is used.
For example, with an ax you can make a house or hurt someone.
Be good and write clean code. Happy coding!
Don't see the point in multiple inheritance when you have interfaces tbh
@@azaria_phd Interface is a subset of inheritance, in inheritance you "inherit" features while in an interface you get a blueprint of what needs to be done, not how. Both are useful, it's up to the system architect to decide the best usage and implementation of the problem being solved.
Great video! Great approach to teaching C++, I am enjoying!
This is an incredible video! Thank you so much!
"Not many people have a stomach like you"
**casually eating**
Very useful vid, thanks. That "mostly true" macro is priceless.
Thanks a lot for this very well explained video
Clear examples, clear explanations... i wish i had a teacher in programming langage that had your pedagogy :)
well done sir :)
Approved
These "don't" videos are awesome. I learned not to use "using namespace std" the hard way when I was first learning C++ in an intro to computer graphics course. Aside from transformation functions from the glm library clashing with my own functions (ie rotate(), scale(), etc), I think I also had an issue where I couldn't make a mathematical vector type cause it conflicted with std::vector. All in all, fun times!
So you blame the namespace feature instead of your inability to do something simple like prefixing your with "myfunc_" or the like to avoid collisions? Real men of genius here... smh
So you couldn't make your own vector because it conflicted with the standard vector
@@alphaforce6998 it was my first time using c++... I was a noob. Plus I would have prefered a normal name instead of myVector. I've never been fond of the my naming convention.
@@beskamir5977 It was just an example. You can prefix the namespace however you want to. When I use namespaces I treat it like a subdirectory and name the functions/code accordingly... and in general a depth of 1 has been more than sufficient for me..
Great talk. I enjoyed every seconds of it :)
great great job :)
by the way you forgot to point out that void* is also very handy when it comes to creating wrappers in C that can then be used in other languages (creating DLLs to be used by other languages for example)!
Thanks for this video, it was very interresting !
That was really good! Very informative and interesting.
Void* is perfect if a void* from one object/function is known when used by another object/function and you know exactly what type of object it may be so you can cast it to what it needs to be, while at the same time storing as a "generic" pointer.
Some cases interfaces and generic templates cannot help, void* is extremely powerful.