Skip to main content
130 votes
Accepted

If null is a billion dollar mistake, what is the solution to represent a non-initialized object?

The problem isn't null itself. It's the implicit nullability of all object references, as is the case in Java, Python, Ruby (and previously C#, although that picture is changing). Imagine you're using ...
Alexander's user avatar
  • 5,195
129 votes
Accepted

Why F#, Rust and others use Option type instead of nullable types like C# 8 or TypeScript?

The purpose of Null Tracking in general (of which Nullable Types are only one of many different forms), is to somehow regain a modicum of safety (and sanity) in languages that have null references. If ...
Jörg W Mittag's user avatar
77 votes
Accepted

What's wrong with returning null?

Why are you getting the warning? You have enabled the nullable reference types (NRT) feature of C#. This requires you to explicitly specify when a null may be returned. So change the signature to: ...
David Arno's user avatar
  • 39.6k
75 votes

Why assert for null on a object before asserting on some of its internals?

Does checking for null has any benefits in the example provided ? The only benefit I see from doing this is a somewhat clearer message on why the test failed. You have successfully answered your own ...
Philip Kendall's user avatar
72 votes
Accepted

Is a new Boolean field better than a null reference when a value can be meaningfully absent?

I don't see why, if you have a meaningfully absent value, null should not be used if you are deliberate and careful about it. If your goal is to surround the nullable value to prevent accidentally ...
TZHX's user avatar
  • 5,072
65 votes

Is a new Boolean field better than a null reference when a value can be meaningfully absent?

nulls aren't evil. Using them without thinking is. This is a case where null is exactly the correct answer - there is no date. Note that your solution creates more problems. What is the meaning of ...
Tom's user avatar
  • 766
43 votes

If nulls are evil, what should be used when a value can be meaningfully absent?

Lots of things are better to return than null. An empty string ("") An empty collection An "optional" or "maybe" monad A function that quietly does nothing An object ...
candied_orange's user avatar
36 votes

C# 8 non-nullable references and the Try pattern

If you're arriving at this a little late, like me, it turns out the .NET team addressed it through a bunch of parameter attributes like MaybeNullWhen(returnValue: true) in the System.Diagnostics....
Nick Darvey's user avatar
34 votes

Why F#, Rust and others use Option type instead of nullable types like C# 8 or TypeScript?

NULL is Overloaded. NULL simultaneously means: This variable has not been initialised This variable has been initialised, but does not point to a valid object, and as such is invalid This variable ...
Kain0_0's user avatar
  • 16.6k
29 votes

Is a new Boolean field better than a null reference when a value can be meaningfully absent?

Depending on your programming language, there might be good alternatives, such as an optional data type. In C++(17), this would be std::optional. It can either be absent or have an arbitrary value of ...
dasmy's user avatar
  • 473
26 votes

Is there a compelling reason why columns in SQL are nullable by default?

At Uni I was taught that the opposite is true. It's much more dangerous to make something not null without reason. With a nullable field the worst thing that can happen is you trip over the ...
Philip Couling's user avatar
25 votes

Must getters return values as is?

Must getters return values as is? No. You are allowed to practice data hiding and encapsulate beyond simply running access to fields through behaviorless getters and setters. We have entities with ...
candied_orange's user avatar
24 votes

Why F#, Rust and others use Option type instead of nullable types like C# 8 or TypeScript?

Much of the angst over nulls are due to languages where every reference type is nullable by default. But this is not an issue for Typescript or C# 8 so lets disregard that. There are two basic ...
JacquesB's user avatar
  • 62.3k
22 votes

Why assert for null on a object before asserting on some of its internals?

Code should be unambiguous where possible If a random NullPointer is thrown in a test, I'm starting to investigate if the program is at fault, or if the writer of the test did just miss the fact of a ...
Falco's user avatar
  • 1,379
22 votes

Does 3-valued logic ever provide practical benefits over 2-valued logic?

Is there any practical value to this complexity? You can already avoid NULLs by making every storage column NOT NULL and using only INNER JOINs for processing. The problem is that most data simply ...
Steve's user avatar
  • 12.6k
21 votes

If nulls are evil, what should be used when a value can be meaningfully absent?

null is not really the problem here. The problem is how most current programming languages deal with missing information; they do not take this problem seriously (or at least dont provide the support ...
marstato's user avatar
  • 4,638
20 votes

What's wrong with returning null?

David Arno already answered your question about the specific warning, so I'd like to address your general question: What's wrong with returning null? Nothing, as long as the consumer of your method ...
Heinzi's user avatar
  • 9,868
20 votes

Does 3-valued logic ever provide practical benefits over 2-valued logic?

This is not a bug. It's a feature. Having no information is different from having a neutral value. NULL is not 0. NULL means there is no value known: If you expect for a column that NULL would be ...
Christophe's user avatar
  • 82.2k
19 votes

If nulls are evil, what should be used when a value can be meaningfully absent?

I do not see any merit in the statement that null is bad. I checked the discussions about it and they only make me impatient and irritated. Null can be used wrong, as a "magic value", when it ...
Martin Maat's user avatar
  • 18.6k
14 votes
Accepted

C# 8 non-nullable references and the Try pattern

The bool/out-var pattern doesn't work nicely with nullable reference types, as you describe. So rather than fight the compiler, use the feature to simplify things. Throw in the improved pattern ...
David Arno's user avatar
  • 39.6k
14 votes

Must getters return values as is?

This is a bit of an X/Y problem and my answer has just a pinch of Frame Challenge. I see some hints at the solution and the real problem: Besides, replacing getInt() with getIntNullable() may cause (...
Greg Burghardt's user avatar
12 votes

Must getters return values as is?

The only reason I see for those methods to be deprecated is getters, I believe, are supposed to return values as is. Including any mapping logic in them feels kind of wrong. That argument may be a bit ...
JimmyJames's user avatar
  • 30.9k
11 votes
Accepted

Advice for bugfixing object oriented code: why is data not set?

There are two ways to deal with object state: Have extremely sophisticated debugging skills so that you can find obscure bugs, or Manage your state in a better way so that obscure bugs do not happen ...
Robert Harvey's user avatar
11 votes
Accepted

if null is bad how we justify the "rfc" nullable for php language?

Nulls are “bad” because many languages make every type implicitly nullable – that weakens the type system considerably as every operation may or may not result in a null pointer exception. However, ...
amon's user avatar
  • 136k
11 votes

Is a new Boolean field better than a null reference when a value can be meaningfully absent?

Correctly using null There are different ways of using null. The most common and semantically correct way is to use it when you may or may not have a single value. In this case a value either equals ...
Gherman's user avatar
  • 945
11 votes

Why F#, Rust and others use Option type instead of nullable types like C# 8 or TypeScript?

Pass me a null and I have to check for null to avoid throwing an exception. Pass me an option, or an empty collection, or a null object, and I can avoid needing the check. I can use it the same as ...
candied_orange's user avatar
11 votes

Why F#, Rust and others use Option type instead of nullable types like C# 8 or TypeScript?

Nullable types need 3 states in order to be safe and useful: Null. Unknown if it is null or not. Definitely not null. Safe to assign to a non-nullable. You can only encode two of those states at ...
Karl Bielefeldt's user avatar
11 votes
Accepted

Is it bad practice to use nullptr in ternary operation?

This is not a good idea because the ternary operator is intend to be used as an expression, and the recommended C++ core guideline is to avoid complicated expressions. Here you only use only the ...
Christophe's user avatar
  • 82.2k
11 votes

Is it bad practice to use nullptr in ternary operation?

In C++23 the code will not compile, as constructing a std::string from nullptr is explicitly disabled. Before that, constructing a std::string from a null pointer (of type const CharT*) caused ...
Deduplicator's user avatar
  • 9,309
10 votes
Accepted

As API author, should I treat Empty and Null the same in search criteria?

You have to answer the question: "Is there a difference between an empty title and an unknown title?" in your context. In a search context, an empty title could mean you're only searching for persons ...
Pieter B's user avatar
  • 13.3k

Only top scored, non community-wiki answers of a minimum length are eligible