What is the purpose of null?

后端 未结 25 2341
别那么骄傲
别那么骄傲 2020-12-07 22:24

I am in a compilers class and we are tasked with creating our own language, from scratch. Currently our dilemma is whether to include a \'null\' type or not. What purpose do

相关标签:
25条回答
  • 2020-12-07 23:21

    null is a sentinel value that is not an integer, not a string, not a boolean - not anything really, except something to hold and be a "not there" value. Don't treat it as or expect it to be a 0, or an empty string or an empty list. Those are all valid values and can be geniunely valid values in many circumstances - the idea of a null instead means there is no value there.

    Perhaps it's a little bit like a function throwing an exception instead of returning a value. Except instead of manufacturing and returning an ordinary value with a special meaning, it returns a special value that already has a special meaning. If a language expects you to work with null, then you can't really ignore it.

    0 讨论(0)
  • 2020-12-07 23:21

    Null is not the problem - everyone treating, and interpreting null differently is the problem.

    I like null. If there was no null, null would only be replaced with some other way for the code to say "I have no clue, dude!" (which some would write "I have no clue, man!", or "I have not a clue, old bean!" etc. and so, we'd have the exact same problems again).

    I generalize, I know.

    0 讨论(0)
  • 2020-12-07 23:23

    Null: The Billion Dollar Mistake. Tony Hoare:

    I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. In recent years, a number of program analysers like PREfix and PREfast in Microsoft have been used to check references, and give warnings if there is a risk they may be non-null. More recent programming languages like Spec# have introduced declarations for non-null references. This is the solution, which I rejected in 1965.

    0 讨论(0)
  • 2020-12-07 23:26

    Null is not a mistake. Null means "I don't know yet"

    For primitives you don't really need a null (I have to say that strings (in .NET) shouldn't get it IMHO)

    But for composite entities it definitely serves a purpose.

    0 讨论(0)
  • 2020-12-07 23:26

    Consider the examples of C and of Java, for example. In C, the convention is that a null pointer is the numeric value zero. Of course, that's really just a convention: nothing about the language treats that value as anything special. In Java, however, null is a distinct concept that you can detect and know that, yes, this is in fact a bad reference and I shouldn't try to open that door to see what's on the other side.

    Even so, I hate nulls almost worse than anything else.

    CLARIFICATION based on comments: I hate the defacto null pointer value of zero worse than I hate null.

    Any time I see an assignment to null, I think, "oh good, someone has just put a landmine in the code. Someday, we're going to be walking down a related execution path and BOOM! NullPointerException!"

    What I would prefer is for someone to specify a useful default or NullObject that lets me know that "this parameter has not been set to anything useful." A bald null by itself is just trouble waiting to happen.

    That said, it's still better than a raw zero wandering around loose.

    0 讨论(0)
  • 2020-12-07 23:26

    A practical example of null is when you ask a yes/no question and don't get a response. You don't want to default to no because it might be important to know that the question wasn't answered in situations where the answer is very important.

    0 讨论(0)
提交回复
热议问题