What would we do without NULL?

后端 未结 11 919
迷失自我
迷失自我 2021-02-01 05:26

I once read that having nullable types is an absolute evil. I believe it was in an article written by the very person who created them(in Ada?) I believe this is the article

11条回答
  •  孤街浪徒
    2021-02-01 05:56

    We'd create all kinds of strange constructs to convey the message of an object 'being invalid' or 'not being there', as seen in the other answers. A message that null can convey very well.

    • The Null Object pattern has its disadvantages, as I explained here.
    • Domain-specific nulls. This forces you to check for magic numbers, which is bad.
    • Collection wrappers, where an empty collection means 'no value'. Nullable wrappers would be better, but that doesn't differ much from checking for null or using the Null Object pattern.

    Personally, I would write some C# preprocessor that allows me to use null. This would then map to some dynamic object, which throws a NullReferenceException whenever a method is invoked on it.

    Back in 1965, null references may have looked like a mistake. But nowadays, with all kinds of code analysis tools that warn us about null references, we don't have to worry that much. From a programming perspective null is a very valuable keyword.

提交回复
热议问题