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.

Filter by
Sorted by
Tagged with
18 votes
8 answers
4k views

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

Let's consider the following test. [Fact] public void MyTest() { // Arrange Code var sut = new SystemWeTest(); // Act Code var response = sut.Request(); // Assert ...
  • 306
3 votes
3 answers
299 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, ...
  • 161
2 votes
3 answers
180 views

User's comment field on a row in relational databases

Suppose that we have a SQL relational database for, let's say, asset management system. It uses a table of assets (1 row per one real-world object). There can be various metadata etc. To allow the ...
  • 191
46 votes
9 answers
19k 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 ...
  • 659
-2 votes
1 answer
72 views

What do you call a function that properly remains undefined?

Backstory: I have subclasses that are supposed to override and define various functions, but not neccessarily all of them. They can't remain purely virtual though for obvious reasons, and so I am ...
  • 3,495
-1 votes
2 answers
1k 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 ...
-2 votes
3 answers
341 views

Why `struct` unconstrained method fails to accept `null` as `T?` parameter

Considering example below, one observe that for reference type argument is successfully understood as nullable parameter. For value type conversion to T?/Nullable<T> fails. T Method<T> ( T?...
  • 288
0 votes
2 answers
333 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 ...
4 votes
1 answer
3k views

What are the advantages of checking for objects with Objects.nonNull()?

Our project changed the object existence checks from foo == null or foo != null to Objects.isNull(foo) or Objects.nonNull(foo). I never got an explanation why that should be better, that's why I ask. ...
0 votes
1 answer
126 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&...
  • 579
60 votes
10 answers
12k views

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

This comes with a debate with my colleague that I'm using null as an object initial state. type Value = Node | null const [value, setValue] = React.useState<Value>(null) function test(v: Value) ...
  • 579
1 vote
1 answer
202 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 ...
  • 589
1 vote
5 answers
898 views

Is this an anti pattern or misusing the interface default method?

Instead of checking the null and throwing exception each time we call findByOrderNumber method, I came up with this pattern by taking advantage of a default method, are there any patterns misused here ...
91 votes
11 answers
19k 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 ...
0 votes
3 answers
484 views

How to inherit a static function in a class?

Probably the answer is you can't. However, I would like to have a work-around to solve my problem. Objective I am trying to create a program in which I try to avoid nulls as much as possible. ...
3 votes
5 answers
5k 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 ...
  • 187
3 votes
4 answers
286 views

Should a transformation function take a nullable and return a nullable or should the caller handle nullability

Often I need to transform a type to another, such as a networking model to a data model, or a data model to a binary representation. Should these transformation functions take an Optional/nullable ...
1 vote
1 answer
123 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(...
  • 113
1 vote
1 answer
541 views

How to handle adding new field / partial data update for a gRPC API?

Assuming we have following message that will be used to update data and it just got updated to version 2. message HelloRequest { string name = 1; // version 1 bool is_valid = 2; // version 2 } ...
0 votes
0 answers
161 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 ...
-1 votes
3 answers
236 views

When to use a reserved word, null or undefined as a key in an object?

I have a case where I have a dictionary object and a value with no key. The object can have the system values and then a user value. I have to store that value. I could use a reserved word or I ...
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 ...
  • 5,652
26 votes
3 answers
4k views

C# 8 non-nullable references and the Try pattern

There is a pattern in C# classes exemplified by Dictionary.TryGetValue and int.TryParse: a method that returns a boolean indicating success of an operation and an out parameter containing the actual ...
-2 votes
1 answer
567 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 <...
  • 105
9 votes
1 answer
5k views

Is there a convention for the Optional/Maybe monad in typescript?

Let's take the classical example of a function that may return a number or not. In typescript this can be represented like this: function f(): number | undefined {} A more elaborate way would be to ...
-1 votes
2 answers
176 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 ...
  • 109
-2 votes
2 answers
375 views

How to have zero or one of and object in a class? [closed]

I want a class A, and I want the relation that A can have zero or one X. So I wrote this class: public class A { private X x = null; public A() {} public A(X x) { this.x = x; } ...
  • 1,418
0 votes
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() { ...
1 vote
1 answer
209 views

How to set nullable/optional property of database object to NULL in backend?

We are building API which mainly passes database objects back and forth between user and database, so the main flow of information is quite basic: Table (view) <-- ORM --> C#/Java/etc. Objects &...
3 votes
1 answer
258 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 ...
1 vote
3 answers
3k views

Setter with one parameter and null default value

Which solution is most logical? The value can be null, but when not null it must be a string. This (First): function setValue(string $value = null); To me this is bad; since we can now call the ...
  • 87
35 votes
14 answers
7k views

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

This is one of the rules that are beaing 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,517
2 votes
3 answers
11k views

How to avoid != null statements? [duplicate]

I use code object != null to avoid NullPointerException. Is there a good alternative to solve this as follow ? if (someobject != null) { someobject.doCalc(); } This will not work for ...
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 ...
  • 270
0 votes
2 answers
2k views

Should I throw ArgumentNullException and should I test it? [duplicate]

Let's take an example of class with 3 dependencies and one method. class Example { private readonly IDependency1 _d1; private readonly IDependency2 _d2; private readonly IDependency3 _d3;...
-1 votes
1 answer
281 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 ...
8 votes
8 answers
812 views

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

As a CS student, I've learned a decent number of programming languages over the years, most of which have had some concept of a "nullable" or "optional" type. Note that I'm not talking about null ...
2 votes
3 answers
197 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>...
5 votes
4 answers
849 views

Return NULL in case of success

This question is a clearer version of a question I posted on SO. I have a C++ Planner object with a method that computes a Route from a start point to a destination point. Planner is the owner of the ...
  • 159
1 vote
2 answers
277 views

Setting a pointer to NULL on failure?

I've been modifying some code written by a previous employee and came across a function with the following signature: BOOL WINAPI PrependPadding( _In_ SIZE_T cbPadding, _In_ SIZE_T cbRow, ...
3 votes
4 answers
276 views

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

When debugging object oriented (Java) code, sometimes some field of some object is null while it should not be. I often spend what I feel is way too much time trying to figure out where it should have ...
  • 652
1 vote
1 answer
369 views

Setters and checking on null value

We are dealing with a lot of null values, now and we are wondering if there are any standards for avoiding it at some places in the code. For example I have this PHP code: class X { ...
  • 87
2 votes
0 answers
392 views

What programming language first used the 'Safe navigation operator' (?.)?

What programming language first used / invented the 'Safe navigation operator' (?.) ? Unfortunately, the Wikipedia page on this subject does not have information on it and googling yielded no readily ...
  • 136
12 votes
1 answer
482 views

What are some intuitions that support calling the Maybe constructor in Haskell "Just"?

The intuition of an optional type like Maybe Int is that either there is no Int (thus, there's Nothing there) or that there is some Int; there is something there. It makes sense to me that we call the ...
  • 276
1 vote
2 answers
7k views

Unit tests for constructors

If I make a class, that in the constructor accepts an array or collection, but this array or collection is optional (the parameter can be null), and then I store that collection in the object in ...
2 votes
6 answers
3k views

When do the benefits of nullable value types outweigh the cost of null pointers?

The question is in the title. Here is the context: Some people think that the null pointer is a big mistake. Tony Hoare famously apologized for inventing it. Since version 2.0 C# has had nullable ...
18 votes
4 answers
9k views

Fetching a value without having to null check in Java

Many times I find myself null checking when fetching a value from some data hierarchy to avoid NullPointerExceptions, which I find to be prone to errors and a needs a lot of boilerplate. I've written ...
5 votes
1 answer
4k views

Is it possible to make nonnull become part of C++ type system?

NULL is the billion-dollar mistake but there is nothing in the type system of C++ to prevent it. However, C++ already has const-correctness so implementing NULL-correctness seems trivial: introduce a ...
2 votes
5 answers
2k views

What value does the byte pointed by null pointer have?

I know that long time ago computer scientists decided to treat all pointers to memory cell of address 0 as NULL. However, the memory cell at that address does exists after all, right? In that case, ...
  • 139
1 vote
2 answers
180 views

Should I return true in a method that casts null from one object type to another?

I'm writing a very simple extension method that attempts to cast objects from one type to another. The intent of having this method is very similar to Int32.TryParse(string, out int), which allows the ...
  • 188