Comments are bad, do this instead!
ฝัง
- เผยแพร่เมื่อ 9 ก.พ. 2025
- In this video, I will show you how to use asserts.
#cpp #programming
Join my Discord:
/ discord
Check out How I Write C++ ✒️
• Why I write C++ like i...
Or How I handle Memory in C++ (no more leaks) 💦
• Finally solve memory l...
Use my assertion function 👍
Note I only wrote the windows implementation, for Linux there is just a place holder, so it compiles on Linux.
Also note that you need to #define INTERNAL_BUILD 1
to allow for debugging features for the assert for the developer. You can just do that at the top of the file. Setting it to 0 won't allow the user to ignore the assert or to debug.
github.com/mee...
Wishlist Midnight Arrow:
store.steampow...
Join this channel if you want to support me 😻:
/ @lowlevelgamedev9330
please never change your accent its so iconic lmao
😂😂 thanks I won't
romanian accent for the win
Yoooo didn't expect to see you here too lol
"If your code needs comments, probably you should improve it"
-some smart guy
"If your code needs variable and function names instead of just randomly generated unique ids, you should improve it"
-some smarter guy
@@11pasa11?
This channel is the only thing that makes making your own game engine possible, thank you for vids like these
thanks 💪 maybe I should make more videos on the make a game engine topic
the cherno be like:
I will use my last 400 megabytes of mobile data to watch this video
real chad cpp enjoyer 💪
How many times are you rewatching that video
Once, but my stupid brain forgot to set it to low res i still have some
@@hcn6708 i dont know how many breads have you eaten in your lifetime?
any property testing is always appreciated. I did write a simple cache and averaged an assert every two function. My test compiled at first (cpp) but didn't pass any test because of my asserts. After I finished all tests passed with correct output. It is alot easier to fix an error that failed on an assert than an error in the output and in case of a cache I would have had to step on each state and check by hand
ive started using asserts too recently, pretty useful stuff
Dude I'm in love with your content
I didn't know C++ had an assert function, thank you!
It’s a macro not a function technically, but yes. There is also static_assert, which is a keyword and is completely different
It's part of the C heritage of C++. My knowledge is way out of date at this point in time, but when I was learning C++, you had to include the header to use it, at least in MSVS.
I recently (a few months ago) started using asserts constantly. Especially with Python, because the lack of strict typing would make me lose my mind otherwise lol
wouldn't be boring to see this function or guess where to put this function exactly? I'll probably forget it but it is still good for bug search.
I like to think it this way. In order, here's what you should do:
1. Write your code so that you don't need assertions.
2. Use static assertions whenever possible. I don't remember if C++ has them, but in Rust I also use generic trait bounds.
3. Use runtime assertions to check dynamic conditions even in release mode.
4. Don't use assertions only on specific performance-bound functions. However, this requires thoroughly testing the function. In Rust, you can also mark functions as unsafe, meaning you have to explicitly call them inside an unsafe block like this: 'unsafe { foo() }'. This is to ensure the programmer is aware that the function being called is inherently dangerous and requires special attention. As far as I know, C++ doesn't have any such feature, so you should add a doc comment to such a function or name it accordingly like 'int unsafe_foo()'.
C++ has static_assert, and you can make a similar thing using C macros as well.
Jonathan Blow does not hate comments. In fact he writes pretty long and detailed blocks of comments in his code. This video is pretty informative, but that bit is just not true.
ye well that's why I said that I was over exagerating
yeah asserts are just good practice in general
i use them in lua, really helps when you accidentally insert a parameter different than it should be and dont realize it
oh yeah they are particularly usefull for loosely typed languages
TBF, that's what exceptions are supposed to be for, so I always make my own assert macro wrapper over a throw, bundling some program state with the exception to make debuging easier
no they mean different things. You are not supposed to "catch" asserts, also you may want to disable them in production. So an invalid input is an exception but a bug in your code is an assert. Also as shown in the video you have the option to ignore an assert or debug it
@@lowlevelgamedev9330 the option to ignore an assert or debug it is a platform-specific VisualStudio thing. On POSIX systems *assert* eventually calls *abort* , which raises a SIGABRT signal. And you CAN 'catch' a signal via hooking a custom signal handler callback to that signal.
asserts are also good for avoiding if else blocks when checking for NULL
if (!var) return
3:56 lol
Maybe some code review videos? 🤫
Lol I was just about to look this up
Static assert only can use constexpr capable functions in it
Isn't assert in release mode basically the same as normal error handling / throwing exceptions ?
No, normally asserts don't run in release mode, also asserts can't be caught. They represent a critical programmer error that you can't recover from.
You can certainly make your own macro that throws a std::runtime_error with the contained message being very similar. Just check the implementation of assert and use the same preprocessor directives to make your release code work to throw an exception.
My comments discuss only the most profound mysteries of the universe and are not concerned by such lowly things as code
those are some cool comments 😎
i have a question, for code in C++, which better? windows or linux?
yo, well depends, in linux it is easy to install libraries but you on the other hand don't have visual studio, and visual studio has the best debugger. You can use clion for linux however (tho I think it is not free) Also you probably should use windows if you want gamedev. But in the end I think you should just use what you like most. You can do cpp well in both windows and linux.
@@lowlevelgamedev9330 Linux full time user here, 100% agree (although I've never used Visual Studio)
@@lowlevelgamedev9330it's easy to install libraries on linux, but when you have to actually distribute your app that uses libraries things start to suck (hence why there are 3 different containerized formats that try to solve this problem, each with their own upsides and downsides). oh, and by the way, a lot of the linux world is ideologically opposed to static linking, so don't even think about trying that.
Today, I have too much cpped
never too much cpp
@@lowlevelgamedev9330 Byte arrays, memcpy, variadic recursive template functions, void* magic, concepts.
Yes, I did do to much cpp today lmao
What happened to your devlogs?
yo I didn't work on any project so that's why I didn't post. But don't worry the Minecraft clone is not dead, it will just take some time untill I post again on that
we loop over the counter 10 times
Vă mulțumesc pentru video! (Româna mea nu este perfectă pentru că sunt ucraineancă, dar cred că ar trebui să vă mulțumesc)
Cmakemakemakemakemake
ekamcekamcekamcekamc
what kind of a programmer doesn't know asserts
Me 😎
New kind
Start using Rust in your code to ×2.3 its quality!
!(p != m) left the chat
What are you?