20241002

Research?: When to use assert

Google Testing Blog: To Assert or Not To Assert

At first, people wrote code, and spend a lot of time debugging. Than someone came up with the idea of asserting that some set of things should never happen. Now there are two kinds of assertions, the ones where you assert that an object will never get into on inconsistent state and the ones where you assert that objects never gets passed a incorrect value…

The author continues with some bad-practices of assertions on null, but it's not directly relevant to my scenario.

Stack Overflow - The four purposes of assert

Assume you work on 200,000 lines of code with four colleagues Alice, Bernd, Carl, and Daphne. They call your code, you call their code.

Then assert has four roles:

  1. Inform Alice, Bernd, Carl, and Daphne what your code expects. (…omitted)
  1. Inform the computer what your code expects. (…omitted)
  1. Inform the readers of your code (including yourself) what your code has achieved at some point. (…omitted)
  1. Inform the computer what your code has achieved at some point. (…omitted)

The detailed explanations are very informative, and my thought is very similar to this post.

The simplest idea is that assert can be considered a comment that gets checked. We can be aware when something that should never happen occurs.


Onigiri 300 Meat and potatos 500 Puchao 200 Salad 400 Mac & Cheese 300 Protein shake 200

Total 1900 kcal


MUST:

TODO:


index 20241001 20241003