What would we do without NULL?

后端 未结 11 916
迷失自我
迷失自我 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:57

    Null is not the problem, it is the language allowing you to write code that accesses values that can possibly be null.

    If the language would simply require any pointer access to be checked or converted to a non-nullable type first, 99% of null related bugs would go away. E.g. in C++

    void fun(foo *f)
    {
        f->x;                  // error: possibly null
        if (f)              
        {
            f->x;              // ok
            foo &r = *f;       // ok, convert to non-nullable type
            if (...) f = bar;  // possibly null again
            f->x;              // error
            r.x;               // ok
        }
    }
    

    Sadly, this can't be retrofitted to most languages, as it would break a lot of code, but would be quite reasonable for a new language.

提交回复
热议问题