Java assertions underused

前端 未结 11 2089
独厮守ぢ
独厮守ぢ 2020-12-04 21:16

I\'m wondering why the assert keyword is so underused in Java? I\'ve almost never seen them used, but I think they\'re a great idea. I certainly much prefer the

11条回答
  •  眼角桃花
    2020-12-04 21:34

    I'm not sure why you would bother to write asserts and then replace them with a standard if then condition statement, why not just write the conditions as ifs in the first place?

    Asserts are for testing only, and they have two side effects: Larger binaries and degraded performance when enabled (which is why you can turn them off!)

    Asserts shouldn't be used to validate conditions because that means the behaviour of your app is different at run time when asserts are enabled/disabled - which is a nightmare!

提交回复
热议问题