Why don't we have two nulls?

后端 未结 27 1102
孤独总比滥情好
孤独总比滥情好 2021-02-02 08:38

I\'ve often wondered why languages with a null representing \"no value\" don\'t differentiate between the passive \"I don\'t know what the value is\"

27条回答
  •  梦毁少年i
    2021-02-02 08:41

    In my programming, I recently adopted the practice of differentiating "language null" and "domain null".

    The "language null" is the special value that is provided by the programming language to express that a variable has "no value". It is needed as dummy value in data structures, parameter lists, and return values.

    The "domain null" is any number of objects that implement the NullObject design pattern. Actually, you have one distinct domain null for each domain context.

    It is fairly common for programmers to use the language null as a catch-all domain null, but I have found that it tends to make code more procedural (less object oriented) and the intent harder to discern.

    Every time to want a null, ask yourself: is that a language null, or a domain null?

提交回复
热议问题