Live data from Hacker News

NULL: The worst mistake of computer science? (2015)

lucidchart.com

71–80 of 377 posts

Re: NULL: The worst mistake of computer science? (2015)

#71
post #50

> NULL is a value that is not a value. And that’s a problem. The problem isn't NULL, it's languages not enforcing the necessary checks for the "no data" condition. Option can still be NULL ("None" in rust), wrapping NULL in a struct doesn't provide any safety. The safety of Option wrapper types is from the other language features (like rust's "match") and a stricter compiler that forces the programmer to write the NU…

You might instead argue that the problem is allowing checks for the null condition. If you make comparing a pointer with null crash, or just remove the keyword to make its use non-idiomatic (or only allow it as special initialization syntax), people won’t casually use null to mean something. It’ll serve its role as a pointer value that crashes cleanly if you dereference it.

Re: NULL: The worst mistake of computer science? (2015)

#72
post #31

Earlier quoted context omitted.

Have you ever dealt with the Maybe(Haskell)/Option(F#) types? If not, then you don't understand what's wrong with NULL and how to easily avoid it without much work.

I find Maybe a bad idea. It forces me to write denormalized code when I know that something is not NULL. It's not possible to specify this knowledge as a data structure since data structures are static but context is dynamic. I much prefer the simple NULL sentinel that blows up like an assertion when I made a mistake. That said, there's not very often a need for NULL at all if you structure the code correctly.

> I much prefer the simple NULL sentinel that blows up like an assertion when I made a mistake.

Haskell, for instance, has the 'fromJust :: Maybe A -> A' function that allows you to do just that. It unpacks the Maybe typed value and throws a runtime error if it fails.

Re: NULL: The worst mistake of computer science? (2015)

#73
post #8

Other candidates: - null terminated strings - machine dependent integer widths

Null-terminated strings are useful for buffering values of unknown length (e.g. copying from a network stream). Length-prefixed strings have issues of their own (e.g. what size length prefix to use, efficient encoding of variable-length integer prefixes, what happens if a length-prefixed string's calculated end-position is outside the process' memory space?)

> Null-terminated strings are useful for buffering values of unknown length (e.g. copying from a network stream).

That doesn't seem like a good example. The data arriving over the network arrives length-prefixed, because 0 is a legal byte value for arbitrary data. What do you then gain by throwing away your existing knowledge of the length?

Re: NULL: The worst mistake of computer science? (2015)

#74
post #67

Earlier quoted context omitted.

I disagree. nullptr is directly convertable to a boolean 0: #include int main(int argc, char *argv[]) { printf("%d\n", nullptr == 0); return 0; }

1. You're comparing a null pointer literal with a null pointer constant. You're literally comparing two things guaranteed to be equal. How, exactly, is it surprising that they do? Did you even attempt to understand what I wrote? 2. The specific implementation you have on hand could use zero-valued null pointers, I'm telling you what the standard doesn't say, confirmed by the C FAQ: http://c-faq.com/null/varieties.htm…

I see what you are saying. I mean zero as null constant. I've never even seen anyone try to assign a null value with some integer zero at some memory address. Using the NULL constant in C/C++ is near effortless and 0 just works no matter what the underlying implementation is doing. So for all intents and purposes nullptr is just a casted NULL (0) constant.

Re: NULL: The worst mistake of computer science? (2015)

#75

Progress, it's now a solved problem in modern languages like Rust, Swift or Kotlin See for example: https://kotlinlang.org/docs/reference/null-safety.html

I mean it's been a solved problem since the 70s if not earlier, the problem has always been uptake. And that is not solved e.g. C++ recently introduced std::optional, which is not a type-safe version of a null pointer but is instead a null pointer wrapper for value types.

Re: NULL: The worst mistake of computer science? (2015)

#76
post #47

Earlier quoted context omitted.

I wonder why this gets downvoted - seems pretty reasonable to me...

It's a rather odd argument and it falls apart as soon as you ask about 0 Kelvin.

> it falls apart as soon as you ask about 0 Kelvin

Huh? The whole point of Kelvins is that their zero point is an actual 0 value. That's why Kelvins are a unit and °C aren't.

Q: An object's temperature is 20°C. The object's temperature increases by 10%. What is the new temperature of the object?

A: 49°C.

Re: NULL: The worst mistake of computer science? (2015)

#77
post #27

NULL is a convenient way to map singularities in your model of the problem. I have on a few occasions tried to write NULL-less code, and it adds a good bit of work. - model all possible states for a value - determine appropriate default actions for all types - meaningful place-holder values It's a good exercise, and I think more code should be written this way, but - as an Engineer I'm trying to model just enough of…

> I have on a few occasions tried to write NULL-less code, and it adds a good bit of work.

Really depends on the language. On Java / C#, it's hard to avoid null-checking a lot of stuff. In C++ you will never encounter a null std::string outside of code written by indian students learning on Turbo C++ 3.5. So you don't need to check for anything: if it's a value, it exists.

Re: NULL: The worst mistake of computer science? (2015)

#79
post #37
post #5

Nulls in strongly typed languages can get rather weird but from a C/C++ perspective it is the same as 0. nullptr is just a correctly casted 0.

This is only true in C, which is part of why C is less insane than C++. The two languages should not be conflated anymore.

This is not even true in C. 0 is a "null pointer literal" when used in pointer context, this does not imply that the actual null pointer has a value of zero.

Re: NULL: The worst mistake of computer science? (2015)

#80
post #47

Earlier quoted context omitted.

It's a rather odd argument and it falls apart as soon as you ask about 0 Kelvin.

> it falls apart as soon as you ask about 0 Kelvin Huh? The whole point of Kelvins is that their zero point is an actual 0 value. That's why Kelvins are a unit and °C aren't. Q: An object's temperature is 20°C. The object's temperature increases by 10%. What is the new temperature of the object? A: 49°C.

> That's why Kelvins are a unit and °C aren't

Could I ask that you update Wikipedia with your discovery? Sadly the page appears to be erroneously using the word "unit" all over the place! https://en.wikipedia.org/wiki/Celsius

Also, NIST might benefit from your guidance: https://physics.nist.gov/cuu/Units/kelvin.html

Post reply on HN