对常量使用 .Equals 以避免(保存)空检查
所以这里是 IValueConverter 的标准代码
{
if (value == null)
return null;
if (value.Equals(true))
return Colors.Red;
return null;
}
还有另一种方式:
{
if (true.Equals(value))
return Colors.Red;
return null;
}
因此,通过使用 true.Equals() 我们保存空检查。使用 true.Equals() 或 "Hello".Equals() 类型检查的一般方法和最佳实践是什么?
PS 我的问题是:您对此有何/一般看法:坏/老套还是好/好?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
无论如何,.Equals 方法通常都会检查 null,但如果您尝试通过删除单个 null 检查来优化代码,那么您确实错过了标记。这实际上不会对任何应用程序产生影响。
The .Equals method generally checks for null anyway, but if you're trying to optimize your code by removing a single null check, you're really missing the mark. This is not going to make a difference in virtually any application.
我非常喜欢较短的代码(我的意思是更少的标记,而不是使用难以破译的短标识符)——它通常更容易阅读和理解,更不容易出现错误,并且需要维护的代码更少。
因此,在我的书中,使用 true.Equals 将空检查留给 .Equals 是一个明显的胜利。
在这个特定的示例中,您甚至可以删除重复的 return 和 do,
但有些人可能会发现它的可读性不如 if 语句。
I have a strong preference for shorter code (and by that I mean less tokens, not using undecipherable short identifiers) - it is often more readable and understandable, less bug prone, and gives you less code to maintain.
So leaving the null check to .Equals by using true.Equals is a clear win in my book.
In this particular example, you could even remove the duplicate return and do
but some might find that less readable than the if statement.
首先,您可能应该使用转换器的基泛型类,它有一个
virtual
强类型方法,在这种情况下,您不是在处理object
,而是使用转换器代码中的bool
。如果我们坚持对象价值,我主张使用可读且可维护的代码和短函数来取代 ifs,因为 ifs 将来可能会变得晦涩难懂。
因此,我会使用像
Color.Red
和null
这样的常量,这也使得它们的意图不清楚。另外,定义这两个常量可以帮助在 IValueConverter 的第二个方法中转换回来,尽管方法
IsValueBoolAndTrue
在技术上与true.Equals(value)
所做的事情相同(在其内部它可以实际上调用这个),有一个特殊的方法将帮助将来查看这个的人不进行会破坏代码的“重构”,因为他会查看true.Equals(value)
和没有评论并认为它实际上不合适,或者会认为它的代码不优雅并重构它,而不保持其功能不变。First of all you should be probably using base generic class for converters, which has a
virtual
strongly typed method, in which case you aren't dealing withobject
, but instead with abool
inside your converter code.If we are stuck with object value I am advocate of readable and maintanable code and short functions which replace ifs which can become obscure in future.
So id be using smth like
Color.Red
andnull
are constants which make their intent unclear too. Also having those 2 constants defined can help with converting back in second method of IValueConverterAlthough method
IsValueBoolAndTrue
technically does same thing astrue.Equals(value)
does (inside itself it can call this actually), having a special method will help person, who looks at this in future, not to make a "refactoring" which will break code, because he would look attrue.Equals(value)
with no comments and think that its actually not smth that is suitable or will think that its inelegant code and refactor it, without leaving its funcionality the same.您可以使用静态 NullReferenceException 和诸如
true.Equals(value)
之类的“有趣”调用-us/library/w4hkze5k(v=vs.110).aspx" rel="nofollow">Object.Equals
方法。因为该方法是由Object
实现的,所以它在所有方法中都可用,并且它将为您执行必要的 null 检查:或者如果您不喜欢三元运算符:
You can avoid
NullReferenceException
and "funny" calls liketrue.Equals(value)
by using the staticObject.Equals
method. Because the method is implement byObject
it is available in all methods and it will perform the necessary null checks for you:or if you dislike the ternary operator: