人们为什么在代码中使用魔术值而不是null? [英] Why are people using magic values instead of null in their code?

查看:166
本文介绍了人们为什么在代码中使用魔术值而不是null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在旧版代码和某些.NET开源项目中看到了这一点. 我无法想象这样做的理由.对我来说,仅使用"null"似乎非常容易.

I have seen this in legacy code and in some .NET open source projects. I can't imagine a reason to do this. Just using "null" seems so much easier to me.

示例:

public class Category
{
   int parentID;

   bool HasParent
   {
      get
      {
          return parentID != -1;
      }
   }
}

public class Category
{
   int parentID;

   bool HasParent
   {
      get
      {
          return parentID != null;
      }
   }
}

推荐答案

因为具有"null"值,该类型必须为可为空.这对于引用类型(您定义的任何类和标准库)都很好,并且如果您看,人们会发现人们 do 每当他们有没有值的引用对象时都使用null

Because to have a "null" value, the type must be nullable. This works fine for reference types (any class you define and the standard library), and if you look you'll see that people do use null whenever they have a reference object with no value

Employee employee = Employees.Find("John Smith");
if(employee == null) throw new Exception("Employee not found");

当您使用诸如int,char或float的 value 类型时,就会出现问题.与引用类型不同,引用类型指向内存中其他地方的数据块,这些值是内联存储和操作的(没有指针/引用).

The issue comes when you use the value types like int, char or float. Unlike reference types, which point to a block of data somewhere else in memory, these values are stored and manipulated inline (there is no pointer/reference).

因此,默认情况下,值类型没有空值.在您提供的代码中,parentID为不可能(实际上,您的编译器甚至对此感到惊讶-Visual Studio 2008和2005可能会绘制绿色下划线,并告诉您语句始终为假).

Because of this, by default, the value types do not have a null value. In the code you provided, it is impossible for parentID to be null (I'm actually surprised this even got by your compiler - Visual Studio 2008 and probably 2005 will draw a green underline and tell you that the statement is always false).

为了使int具有空值,您需要将其声明为可空值

In order for an int to have a null value, you need to declare it as nullable

int? parentID;

Now parentID可以包含一个空值,因为它现在是32位整数的 pointer 指针(即引用"),而不仅仅是32位整数

Now parentID can contain a null value, because it is now a pointer (well "reference") to a 32 bit integer, instead of just a 32bit integer.

因此,希望您能理解为什么魔术值"会产生通常用于用基本类型(值类型)表示null.将这些值类型存储为值的引用,以使它们为空,这会很麻烦,并且经常会给性能造成很大的影响(查找装箱/拆箱是什么).

So hopefully you understand why "magic values" are often used to represent null with the basic types (value types). It is simply a lot of trouble, and often a large performance hit (lookup what boxing/unboxing is), to store these value types as a reference to the value in order to allow them to be null.

有关装箱/拆箱(需要int == null)的更多参考,请参见

For further reference about boxing/unboxing (what you need to have an int==null), see the article at MSDN:

装箱和拆箱(C#编程指南)

性能

相对于简单分配,装箱和拆箱是计算上昂贵的过程.装箱值类型时,必须分配和构造一个新对象.在较小程度上,拆箱所需的演员表在计算上也很昂贵.有关更多信息,请参见性能.

In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally. For more information, see Performance.

这篇关于人们为什么在代码中使用魔术值而不是null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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