Skip to main content
124 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
123 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). If you have a ...
Alexander's user avatar
  • 4,954
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.4k
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
64 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
  • 756
41 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
35 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.2k
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
25 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
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
  • 59.8k
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,293
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,628
21 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
  • 9,022
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,798
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.4k
17 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
  • 78.4k
15 votes

Property-coalescing operator for C#

I believe you'll be able to do this with C# 6 quite simply now: return _mywidget?.Length ?? 5; Note the null-conditional operator ?. on the left side. _mywidget?.Length returns null if _mywidget is ...
Chris Nolet's user avatar
14 votes
Accepted

Fetching a value without having to null check in Java

Your solution is very smart. The problem I see is the fact that you don't know why you got a null? Was it because the house had no rooms? Was it becuase the town had no houses? Was it because the ...
Tulains Córdova's user avatar
13 votes

Fetching a value without having to null check in Java

The idea is fine, really good in fact. Since Java 8 the Optional types exist, a detailed explanation can be found at Java Optional type. A example with what you posted is Optional.ofNullable(country) ...
J. Pichardo's user avatar
13 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.4k
12 votes

In C, why is NULL and 0 triggering an if statement

In C, NULL is a macro that expands either to 0 or (void*)0 (or something that has a similar effect). In the first case, you can not differentiate between NULL and 0, because they are literally the ...
Bart van Ingen Schenau's user avatar
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
  • 135k
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
  • 78.4k
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,081

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