Why should I use asserts?

后端 未结 19 2005
遇见更好的自我
遇见更好的自我 2020-12-12 13:47

I never got the idea of asserts -- why should you ever use them?

I mean, let\'s say I were a formula driver and all the asserts were things like security belt, helm

19条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 14:32

    From Code Complete 2: "Use error-handling for conditions you expect to occur; use assertions for conditions that should never occur."

    A commonly-cited example is checking for zero in the denominator before a division.

    You're expected to strip the assertions out of production code. They are in there during development to help you catch mistakes.

    Unit tests are not a replacement for assertions.

提交回复
热议问题