Why don't I need to check if references are invalid/null?

前端 未结 11 2028
半阙折子戏
半阙折子戏 2020-12-23 11:20

Reading http://www.cprogramming.com/tutorial/references.html, it says:

In general, references should always be valid because you must always initi

11条回答
  •  隐瞒了意图╮
    2020-12-23 11:36

    I think it "depends". I know, that is not an answer, but it does really depend. I think coding defensively is a good practice to follow. Now if your stack track is 10 levels deep and any failure down the trace causes the entire operation to fail, then by all means, check at the top level and let any exceptions rise to the top. but if you can recover from someone passing you a null reference, the check where appropriate. In my experience, where I have to bring code together with other companies to integrate together, checking ( and logging ) everything at the public api level lets you deflect the finger pointing that happens when integration does not go as expected.

提交回复
热议问题