Questions tagged [null]
Null is the absence of a value. Null is typically used to indicate that a reference or pointer variable points to no object in memory.
133 questions
8
votes
7
answers
5k
views
Must getters return values as is?
We have entities with numeric properties, they are of boxed types (e.g. Integer). Typically, each of those properties has a pair of getters: getInt(), which returns zero on null values, and ...
2
votes
1
answer
244
views
checkNotNull vs. JEP 358: Helpful NullPointerExceptions: Should we remove existing null checks?
With the introduction of JEP 358 in Java 14, which provides more informative NullPointerException (NPE) messages, is it advisable to remove existing explicit null checks in cases where the null-check ...
0
votes
2
answers
127
views
Modeling value object when fields' existence depends on state of other fields
I am practicing tactical DDD and having trouble as exemplified below. Fundamentally, whether some fields of the value object should be nullable depends on another field of the same value object. ...
5
votes
5
answers
1k
views
Is it bad practice to use nullptr in ternary operation?
I've set up a ternary operator in place of a pile of if-else's, the final expression being nullptr in order to finish the loop, like so:
int menuSelect;
std::string operation="";
(...
1
vote
4
answers
3k
views
Validating data classes with nullable properties that should never be null
When retreiving data with an api and saving it in a DTO, some values are nullable: null on initial class initialization but VS also warns you for this. For example, an employee:
public class ...
4
votes
3
answers
1k
views
Is there a programming language other than Java, C#, and Go which includes null with its static object types?
I was reading the excellent book by Axel Raushmayer, Tackling TypeScript.
In this section of Chapter 7, the author makes the interesting claim
In many programming languages, null is part of all ...
2
votes
4
answers
741
views
Large Inheritance Hierarchy vs. One Object With Many Nullable Fields
I am working on implementing some stock order types for a financial technology application. There are six different types of stock orders - market, limit, stop_loss, stop_loss_limit, trailing_stop, ...
47
votes
8
answers
30k
views
What's wrong with returning null?
I've recenlty been greeted by CS8603 - Possible null reference return, which indicates that my code could possibly return null. It's a simple function that looks up an entity in a database by id - if ...
-1
votes
2
answers
5k
views
Determining the object that caused a null reference exception?
Frequently in applications we encounter situations that could throw a NullReferenceException; for example, assuming the following method's argument is a user defined reference type, accessing the ...
0
votes
2
answers
766
views
DDD Modeling - Entity with generic and specific data
Suppose I am buying coffee. There are several types of coffee (A1, A2, A3), but sometimes I want to make a reference to all types of coffee (like if I had a coffee "grouped"). Considering ...
0
votes
1
answer
178
views
Use nullable or undefineable as object type?
This come with a debate with my colleague that I'm using nullable object type.
type Value = Node | null
const [v0, setV0] = React.useState<Value>(null)
const [v1, setV1] = React.useState&...
2
votes
1
answer
322
views
What is the reasoning behind Kotlin using non-nullable types for Java interop methods?
Considering Kotlin Java Interop: Null Safety and Platform Types
Why is code like this legal in Kotlin?
fun envString(key: EnvVars): String {
return System.getenv(key.toString())
}
getenv() can ...
94
votes
11
answers
21k
views
Why F#, Rust and others use Option type instead of nullable types like C# 8 or TypeScript?
AFAIK, Option type will have runtime overhead, while nullable types won't, because Option time is an enum (consuming memory).
Why not just mark optional references as optional, then the compiler can ...
3
votes
5
answers
12k
views
Java: Why not allow nulls in methods to represent optional parameters?
I wanted to follow up on this previous question I asked related to @Laive comment, but I couldn't think of an excellent way to do so without asking another question, so here we go.
With the previous ...
1
vote
1
answer
177
views
declare 2 classes for nullable / not nullable objects?
Let's say I have an which is loosely can be represented as:
public class AnObject{
public AnObject(String name, String value, UUID id) {
this.name = Objects.requireNonNull(...
0
votes
0
answers
199
views
Should I check for nulls, even though I won't get any, as far as I know? [duplicate]
I'm new to software engineering and right now I'm focused on learning the best practices to consistently write robust code.
Recently I've been maintaining an application built by other people and/or ...
40
votes
8
answers
11k
views
Is a new Boolean field better than a null reference when a value can be meaningfully absent?
For example, suppose I have a class, Member, which has a lastChangePasswordTime:
class Member{
.
.
.
constructor(){
this.lastChangePasswordTime=null,
}
}
whose lastChangePasswordTime ...
-2
votes
1
answer
939
views
Using static member methods to check for object being NULL
Is it a good practice to use the static member methods to check if an object of a class is NULL or not. The object would be sent through the parameters offcourse.
Something like,
#include <...
-1
votes
2
answers
205
views
Null checks good practice if code will only be ran when object is never null
Using a website with Javascript as example.
Let's say I have script A which only performs a specific function on page Foo. For example something like sorting elements in a list.
Script A is only ...
1
vote
2
answers
2k
views
Is it beneficial to throw a caught IOException as an UncheckedIOException in order to prevent NullPointerException?
Not using UncheckedIOException, NullPointerException possible
public void callerMethod() {
Object result = ioMethod();
// call instance method of result
}
public Object ioMethod() {
...
3
votes
1
answer
342
views
if null is bad how we justify the "rfc" nullable for php language?
I do not understand how you think about it:https://wiki.php.net/rfc/nullable_types
when It is widely confirmed, that using nulls is bad practice
Where am I wrong?
thanks.
I'm not criticizing !.
I ...
38
votes
15
answers
11k
views
If nulls are evil, what should be used when a value can be meaningfully absent?
This is one of the rules that are being repeated over and over and that perplex me.
Nulls are evil and should be avoided whenever possible.
But, but - from my naivety, let me scream - sometimes a ...
3
votes
3
answers
3k
views
As API author, should I treat Empty and Null the same in search criteria?
I have a RESTFUL api, one of the endpoints is receiving search criteria which contains property for "Title".
Should I allow consumers to send either null (or eliminate the property) or Empty string in ...
-1
votes
1
answer
351
views
Null Object and Exceptions
Do Special Case or Null Object design patterns still provide value when application behavior, not just object behavior has to change?
I was tasked with revisiting an old application and refactoring ...
2
votes
3
answers
265
views
Is there anything wrong with making nullability of a reference type explicit by wrapping it in a value type?
When I program in Java, I make all nullability explicit; that is, an instance of Foo is assumed to be non-null, and if I want it to be null, I use a @Nullable annotation (or better, Optional<Foo>...