Start using Asserts in your C++ code to ×2.3 its quality!

Поделиться
HTML-код
  • Опубликовано: 18 сен 2024
  • 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) 💦
    • What I do to never hav...
    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

Комментарии • 63

  • @NoVIcE_Source
    @NoVIcE_Source Месяц назад +68

    please never change your accent its so iconic lmao

  • @xijnin
    @xijnin Месяц назад +18

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

    • @11pasa11
      @11pasa11 Месяц назад +3

      "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 Месяц назад

      ​@@11pasa11?

  • @hughjanes4883
    @hughjanes4883 Месяц назад +11

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

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  Месяц назад +5

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

    • @anon1963
      @anon1963 Месяц назад

      the cherno be like:

  • @Cinarbayramic
    @Cinarbayramic Месяц назад +24

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

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  Месяц назад +2

      real chad cpp enjoyer 💪

    • @hcn6708
      @hcn6708 Месяц назад +1

      How many times are you rewatching that video

    • @Cinarbayramic
      @Cinarbayramic Месяц назад +1

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

  • @averilongnem9873
    @averilongnem9873 Месяц назад +4

    ive started using asserts too recently, pretty useful stuff

  • @salim444
    @salim444 Месяц назад +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

  • @iiHaSTe
    @iiHaSTe Месяц назад +1

    Dude I'm in love with your content

  • @lets_get_dev
    @lets_get_dev Месяц назад +4

    keep going❤

  • @natanmaia3575
    @natanmaia3575 Месяц назад

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

    • @Urien.
      @Urien. Месяц назад +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 17 дней назад +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.

  • @luchong11
    @luchong11 Месяц назад +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.

  • @nicholas_obert
    @nicholas_obert Месяц назад +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 28 дней назад +1

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

  • @noname-zt2zk
    @noname-zt2zk Месяц назад

    Lol I was just about to look this up

  • @playervalley
    @playervalley Месяц назад

    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  Месяц назад

      oh yeah they are particularly usefull for loosely typed languages

  • @Raspredval1337
    @Raspredval1337 Месяц назад

    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  Месяц назад +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 Месяц назад

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

  • @bluesillybeard
    @bluesillybeard Месяц назад

    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

  • @arl-t8d
    @arl-t8d Месяц назад

    Maybe some code review videos? 🤫

  • @JFrancoe
    @JFrancoe Месяц назад

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

  • @mahdijoharian2731
    @mahdijoharian2731 Месяц назад

    Static assert only can use constexpr capable functions in it

  • @bionic_batman
    @bionic_batman Месяц назад

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

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  Месяц назад

      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 28 дней назад

      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.

  • @dollengo
    @dollengo Месяц назад

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

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  Месяц назад +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 Месяц назад

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

    • @somenameidk5278
      @somenameidk5278 Месяц назад

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

  • @1000timka
    @1000timka Месяц назад

    we loop over the counter 10 times

  • @nlk294
    @nlk294 Месяц назад

    What happened to your devlogs?

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  Месяц назад

      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

  • @leshommesdupilly
    @leshommesdupilly Месяц назад

    Today, I have too much cpped

    • @lowlevelgamedev9330
      @lowlevelgamedev9330  Месяц назад

      never too much cpp

    • @leshommesdupilly
      @leshommesdupilly Месяц назад

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

  • @marks_shot
    @marks_shot Месяц назад

    Cmakemakemakemakemake

  • @Niohimself
    @Niohimself Месяц назад

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

  • @winchester2581
    @winchester2581 Месяц назад

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

  • @madbanana22
    @madbanana22 Месяц назад

    what kind of a programmer doesn't know asserts

  • @someon3
    @someon3 Месяц назад

    !(p != m) left the chat

  • @lolguy91wastakenbyanidiot
    @lolguy91wastakenbyanidiot Месяц назад

    What are you?

  • @spermakonya
    @spermakonya Месяц назад

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