Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Null considered beneficial

Name: Anonymous 2015-08-03 4:56

The nil or null value was discovered, seemingly independently, when creating Lisp (perhaps IPL too), PL/I, Simula, Algol W, and BCPL (where it was just integer 0).
An all-bits-zero value was already in use by assembly programmers for marking the end of a linked list or tree.
It arises naturally when implementing pointers and linked lists.
Null is a list terminator, an uninitialized value, a dummy object, a null or revoked capability.
Null makes garbage collectors and secure memory allocators possible.

Without pointers, there would still be a null analogue.
Fortran programmers used to use array indices as pointers and a 0 index as null.
Process calculi often have a null process which can be compared by identity.

Null is one of the most noble and powerful discoveries of programming.
Although it is very simple, a full understanding of its power cannot be detailed in such a short post.

Name: Anonymous 2015-09-21 8:56

>>1
Null is a sentinel value. Of all the bit patterns an address can take we reserve one to mean no value. It is a useful implementation strategy like you said, and it is popular because it is so obvious and easy to implement. The same idea is used in C-strings and NULL terminated pointer arrays and linked lists and trees.

But we can do better. The program already logically distinguishes between pointers that point to things and ones that don't. In some places we cannot know whether a result will be successful (e.g. get the next item in the list) so we allow a null pointer to be returned in certain cases. In other places we require that a pointer is not null, because the computation doesn't make sense otherwise. In many languages this turns into function preludes with boilerplate null checks, and forgotten checks gives you exceptions and segfaults.

Therefore an Option/Maybe/etc. type together with pointers that cannot be null is clearly preferable, because intent is expressed in the code. Wherever there is uncertainty you have an optional reference, and you need to handle both cases if you need to access the value that is potentially there. In other places where a value is always present references can be used directly and safely. It's an easy way to make sure there are no missing null checks, and you never accidentally dereference a null pointer again. It is also as cheap as traditional pointers, because the compiler can implement it with a sentinel value if it so pleases.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List