Check if variable null before assign to null?

后端 未结 8 1093
清歌不尽
清歌不尽 2021-01-05 16:55

Is variable assignment expensive compared to a null check? For example, is it worth checking that foo is not null before assigning it null?

if (foo != null)          


        
8条回答
  •  情话喂你
    2021-01-05 17:20

    This is a micro-micro-optimization (and possibly something handled by the compiler anyways). Don't worry about it. You'll get a far greater return by focusing on your programs actual algorithm.

    We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. -- Donald Knuth

提交回复
热议问题