Is Java assert broken?

后端 未结 12 816
走了就别回头了
走了就别回头了 2020-12-29 23:33

While poking around the questions, I recently discovered the assert keyword in Java. At first, I was excited. Something useful I didn\'t already know! A more

12条回答
  •  渐次进展
    2020-12-30 00:20

    Assertions are really a great and concise documentation tool for a code maintainer.

    For example I can write:

    foo should be non-null and greater than 0

    or put this into the body of the program:

    assert foo != null;
    assert foo.value > 0;
    

    They are extremely valuable for documenting private/package private methods to express original programmer invariants.

    For the added bonus, when the subsystem starts to behave flaky, you can turn asserts on and add extra validation instantly.

提交回复
热议问题