检查变量null之前分配null? [英] Check if variable null before assign to null?

查看:203
本文介绍了检查变量null之前分配null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与空检查相比,变量赋值是否昂贵?例如,是否值得检查foo不为空,然后再赋值为null?

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) {
     foo = null;
}

或者这是什么都不担心?

Or is this worrying about nothing?

推荐答案

这是一个微微优化(可能由编译器处理的东西)。不要担心。

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.


我们应该忘记小的效率,说大约97%的时间:过早优化是一切邪恶的根源。 - Donald Knuth

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

这篇关于检查变量null之前分配null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆