Write secure code with assertions (assert and static_assert) | Modern Cpp Series Ep. 87

Поділитися
Вставка
  • Опубліковано 8 вер 2022
  • ►Full C++ Series Playlist: • The C++ Programming La...
    ►Find full courses on: courses.mshah.io/
    ►Join as Member to Support the channel: / @mikeshah
    ►Lesson Description: In this lesson I will show you one way to catch bugs at run-time, and one way to catch bugs at compile-time. The assert statement (from C) is available to us in C++. Assertions serve as contracts of thing that must absolutely be true, and if they're not, the safest thing to do is for our program to terminate. In modern C++ we have available to use static_assert which can check boolean-constexpr expressions to see if they pass the test, and catching bugs at compile-time is often much easier on us in the long run!
    ►UA-cam Channel: / mikeshah
    ►Please like and subscribe to help the channel!
  • Наука та технологія

КОМЕНТАРІ • 17

  • @__hannibaalbarca__
    @__hannibaalbarca__ Рік тому +1

    Thanks; Very Kind small examples “ the big things came out of tiny things”. Thanks again for the methodology of how to understand and learn something.

  • @enriquemorenoroldan8402
    @enriquemorenoroldan8402 Рік тому +1

    Thanks again for your videos Mike!

  • @reptilicusrex4748
    @reptilicusrex4748 Рік тому +1

    Thanks for the great explanation.

  • @GaryChike
    @GaryChike Рік тому +2

    It's just a matter of time before someone comes up with a programming language called "42" 😂 Great explanation Mike!

    • @MikeShah
      @MikeShah  Рік тому

      Cheers! Thanks Gary 😁

  • @VoidloniXaarii
    @VoidloniXaarii 8 місяців тому +1

    Thank you very much. Sorry if stupid question, how do these asserts relate to the ones in Google's test framework asserts... If any?

    • @MikeShah
      @MikeShah  8 місяців тому +1

      Similar idea, you want to assert and catch during testing things that must be true. Google provides an abstraction layer to help make some of the testing ideas more clear.

    • @VoidloniXaarii
      @VoidloniXaarii 8 місяців тому +1

      @@MikeShah thank you!

    • @VoidloniXaarii
      @VoidloniXaarii 8 місяців тому +1

      @@MikeShah thank you so much for all your enlightened teachings!

  • @bsdooby
    @bsdooby Рік тому +1

    How do you setup your C++ code such that it can be tested on an ad hoc basis? E.g. Java has maven with test profiles; I struggle a bit doing something similar with C++...I guess one has to resort to CMake (or similar build environments) to invoke test suits (?) And D has compiler switches which invoke unit tests.

    • @MikeShah
      @MikeShah  Рік тому +2

      Previously I've used Catch framework with a CMake to do a test build, alongside a debug and release build. D's compiler switches are a 'killer feature' in my opinion, and one of the reason you are seeing me make the DLang series :)
      Visual Studio is one IDE I know about that also has built-in test frameworks to emulate what D has (and essentially what Catch does as well). See an example here: docs.microsoft.com/en-us/visualstudio/test/writing-unit-tests-for-c-cpp?view=vs-2022

    • @bsdooby
      @bsdooby Рік тому +1

      Thanks for these valuable hints! Will def. check them out.

  • @fatty-it-man
    @fatty-it-man 5 місяців тому

    Great video! But)
    From my experience in the field, c assert() is a bad thing for the project. In Release variant asserts are compiled out, so you miss the checks. The checks were performed only in Debug variant. So you have different code between Debug and Release variants.
    C asserts can work good only in a big company, where they do extensive testing for debug AND release variants.
    My best practice: just do not use c asserts. If there is a suspect for an existence of an error, perform a mature param check with if (...) {print log, handle error / throw exception} and just avoid using c asserts...
    But static_assert is a great stuff)

    • @MikeShah
      @MikeShah  5 місяців тому +1

      Agree to prefer static_assert when possible. Assert is for developers during development, at run-time in release if errors are handled this is where exceptions are useful. May depend on domain, but I recommend liberal use of assertions -- and you are correct we do not typically want an assert in release that crahes the program 🙂