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 ...
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 ...
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:
...
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 ...
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 ...
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 ...
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 ...
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....
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 (...
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 ...
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 ...
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, ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
null × 133java × 29
c# × 21
exceptions × 12
design × 10
c++ × 10
design-patterns × 9
object-oriented × 6
c × 6
language-design × 6
error-handling × 6
database × 5
programming-practices × 5
coding-style × 5
inheritance × 5
pointers × 5
programming-languages × 4
database-design × 4
api × 4
sql × 4
methods × 4
parameters × 4
defensive-programming × 4
object-oriented-design × 3
domain-driven-design × 3