Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 17
    Actually, the worst thing that happens is when code crashes due to something that is NOT an assert. If the code will definitely crash later on with 100% probability, then the assert must absolutely be there. If you deref a pointer, then you have to implicitly assert that it isn't null before. If you divide by a number, you assert that it's not zero. Take out the asserts and all crash locations are UNDOCUMENTED. The real problem is not structuring the program to let subsystems crash and be restarted by a watchdog. Commented Jun 9, 2014 at 18:50
  • 7
    assert ref != null; is different than if (ref == null) throw new IllegalArgumentException(); You shouldn't be using the first for preconditions that could be false. You need to use assert for things that can't be false. Example, int i = -1 * someNumber; i = i * i; then later to remind people that i is positive, assert i > 0; Commented Sep 9, 2015 at 15:28
  • 1
    "Actually, the worst thing that happens is when code crashes due to something that is NOT an assert. If the code will definitely crash later on with 100% probability, then the assert must absolutely be there." - this is a false dichotomy. Commented Apr 3, 2019 at 12:23
  • 2
    @RobertGrant: For many programs, crashing is far from the worst thing that can happen. For a program that's supposed to check the soundness of a building or bridge design, wrongly reporting that a design is sound can be just about the worst thing it could do. For a program that's exposed to the outside world but has read-only access to confidential data, leaking that data may be worse than just about anything else the program could do. The notion that a crash with no meaningful indication of the cause is "the worst thing that could happen" ignores many dangers that are far worse. Commented Jan 14, 2020 at 21:24
  • @supercat I wasn't agreeing with the comment I was quoting. Commented Jan 15, 2020 at 22:11