Why should I use asserts?

后端 未结 19 2029
遇见更好的自我
遇见更好的自我 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条回答
  •  萌比男神i
    2020-12-12 14:09

    It's a controversial subject. Many people, like myself, do actually prefer to leave them on in production code. If your program is going to go into the weeds anyway, you might as well have the assertion in there so your customer can at least give you the line number and filename (or whatever information or action you configure the assert to do). If you left the assertion out, all the customer could report to you was "it crashed".

    This means you probably should not do expensive operations in your assert checks, or at least profile to see if they are going to cause performance problems.

提交回复
热议问题