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

ความคิดเห็น • 65

  • @NoVIcE_Source
    @NoVIcE_Source 6 หลายเดือนก่อน +72

    please never change your accent its so iconic lmao

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน +24

      😂😂 thanks I won't

    • @AndrieMC
      @AndrieMC 6 หลายเดือนก่อน +7

      romanian accent for the win

    • @sungoner5906
      @sungoner5906 6 หลายเดือนก่อน

      Yoooo didn't expect to see you here too lol

  • @xijnin
    @xijnin 6 หลายเดือนก่อน +20

    "If your code needs comments, probably you should improve it"
    -some smart guy

    • @11pasa11
      @11pasa11 6 หลายเดือนก่อน +5

      "If your code needs variable and function names instead of just randomly generated unique ids, you should improve it"
      -some smarter guy

    • @Joel-zi6pt
      @Joel-zi6pt 6 หลายเดือนก่อน

      ​@@11pasa11?

  • @hughjanes4883
    @hughjanes4883 6 หลายเดือนก่อน +11

    This channel is the only thing that makes making your own game engine possible, thank you for vids like these

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน +5

      thanks 💪 maybe I should make more videos on the make a game engine topic

    • @anon1963
      @anon1963 6 หลายเดือนก่อน

      the cherno be like:

  • @Cinarbayramic
    @Cinarbayramic 6 หลายเดือนก่อน +27

    I will use my last 400 megabytes of mobile data to watch this video

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน +3

      real chad cpp enjoyer 💪

    • @hcn6708
      @hcn6708 6 หลายเดือนก่อน +1

      How many times are you rewatching that video

    • @Cinarbayramic
      @Cinarbayramic 6 หลายเดือนก่อน +1

      Once, but my stupid brain forgot to set it to low res i still have some

    • @whiteingale
      @whiteingale 3 หลายเดือนก่อน

      @@hcn6708 i dont know how many breads have you eaten in your lifetime?

  • @salim444
    @salim444 6 หลายเดือนก่อน +2

    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

  • @averilongnem
    @averilongnem 6 หลายเดือนก่อน +4

    ive started using asserts too recently, pretty useful stuff

  • @iiHaSTe
    @iiHaSTe 6 หลายเดือนก่อน +1

    Dude I'm in love with your content

  • @natanmaia3575
    @natanmaia3575 6 หลายเดือนก่อน

    I didn't know C++ had an assert function, thank you!

    • @Urien_n
      @Urien_n 6 หลายเดือนก่อน +1

      It’s a macro not a function technically, but yes. There is also static_assert, which is a keyword and is completely different

    • @VTdarkangel
      @VTdarkangel 5 หลายเดือนก่อน +1

      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.

  • @bluesillybeard
    @bluesillybeard 6 หลายเดือนก่อน +1

    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

  • @whiteingale
    @whiteingale 3 หลายเดือนก่อน

    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.

  • @nicholas_obert
    @nicholas_obert 5 หลายเดือนก่อน +1

    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()'.

    • @brianb2308
      @brianb2308 5 หลายเดือนก่อน +1

      C++ has static_assert, and you can make a similar thing using C macros as well.

  • @luchong11
    @luchong11 6 หลายเดือนก่อน +2

    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.

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน +1

      ye well that's why I said that I was over exagerating

  • @playervalley
    @playervalley 6 หลายเดือนก่อน

    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

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน

      oh yeah they are particularly usefull for loosely typed languages

  • @Raspredval1337
    @Raspredval1337 6 หลายเดือนก่อน

    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

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน +2

      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

    • @Raspredval1337
      @Raspredval1337 6 หลายเดือนก่อน

      @@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.

  • @JFrancoe
    @JFrancoe 6 หลายเดือนก่อน

    asserts are also good for avoiding if else blocks when checking for NULL

    • @benshulz4179
      @benshulz4179 5 หลายเดือนก่อน

      if (!var) return

  • @DynestiGTI
    @DynestiGTI 21 วันที่ผ่านมา

    3:56 lol

  • @arl-t8d
    @arl-t8d 6 หลายเดือนก่อน

    Maybe some code review videos? 🤫

  • @noname-zt2zk
    @noname-zt2zk 6 หลายเดือนก่อน

    Lol I was just about to look this up

  • @mahdijoharian2731
    @mahdijoharian2731 6 หลายเดือนก่อน

    Static assert only can use constexpr capable functions in it

  • @bionic_batman
    @bionic_batman 6 หลายเดือนก่อน

    Isn't assert in release mode basically the same as normal error handling / throwing exceptions ?

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน

      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.

    • @brianb2308
      @brianb2308 5 หลายเดือนก่อน

      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.

  • @Niohimself
    @Niohimself 6 หลายเดือนก่อน

    My comments discuss only the most profound mysteries of the universe and are not concerned by such lowly things as code

  • @dollengo
    @dollengo 6 หลายเดือนก่อน

    i have a question, for code in C++, which better? windows or linux?

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน +4

      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.

    • @bluesillybeard
      @bluesillybeard 6 หลายเดือนก่อน

      @@lowlevelgamedev9330 Linux full time user here, 100% agree (although I've never used Visual Studio)

    • @somenameidk5278
      @somenameidk5278 6 หลายเดือนก่อน

      ​@@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.

  • @leshommesdupilly
    @leshommesdupilly 6 หลายเดือนก่อน

    Today, I have too much cpped

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน

      never too much cpp

    • @leshommesdupilly
      @leshommesdupilly 6 หลายเดือนก่อน

      @@lowlevelgamedev9330 Byte arrays, memcpy, variadic recursive template functions, void* magic, concepts.
      Yes, I did do to much cpp today lmao

  • @nlk294
    @nlk294 6 หลายเดือนก่อน

    What happened to your devlogs?

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  6 หลายเดือนก่อน

      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

  • @1000timka
    @1000timka 6 หลายเดือนก่อน

    we loop over the counter 10 times

  • @winchester2581
    @winchester2581 6 หลายเดือนก่อน

    Vă mulțumesc pentru video! (Româna mea nu este perfectă pentru că sunt ucraineancă, dar cred că ar trebui să vă mulțumesc)

  • @marks_shot
    @marks_shot 6 หลายเดือนก่อน

    Cmakemakemakemakemake

  • @madbanana22
    @madbanana22 6 หลายเดือนก่อน

    what kind of a programmer doesn't know asserts

    • @spiderpig20
      @spiderpig20 6 หลายเดือนก่อน

      Me 😎

    • @sirmewo5510
      @sirmewo5510 4 หลายเดือนก่อน

      New kind

  • @frontenduser
    @frontenduser 6 หลายเดือนก่อน

    Start using Rust in your code to ×2.3 its quality!

  • @someon3
    @someon3 6 หลายเดือนก่อน

    !(p != m) left the chat

  • @lolguy91wastakenbyanidiot
    @lolguy91wastakenbyanidiot 6 หลายเดือนก่อน

    What are you?