Jump to content

Null values C#, SQL

pixeledadler

Hello dear coders

This one thing doesn't leave my mind:
Is it good practice to avoid null values?

The argument being, one can avoid uncertainty, when variables always have a certain value.
The contra argument: it uses more resources.

 

What do you say? Do you practice using certain values everywhere?

Link to comment
Share on other sites

Link to post
Share on other sites

It really depends. Avoiding null values is more about avoiding unnecessary null checks and potential null pointer exceptions, not about resource usage.

 

But sometimes null is the only sensible value for something where you don't know a value (yet).

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, pixeledadler said:

@Eigenvektor Yeah. Hate them null checks. This would also mean, every object must have a full constructor.

Avoiding nulls can be easy if you have sensible default values or values that are obviously out of bounds. For example if you have an integer value that represents some kind of count, you can often use -1 to indicate that the value isn't known yet.

 

But if your integer can take any value that is no longer an option. You could work around that by adding an additional boolean field to indicate whether the value has been initialized or not, but at that point you can also just make it nullable. I'd rather have a nullable column in my database than an additional boolean column.

 

For service classes you can sometimes provide default implementations that simply don't do anything. So you don't need to check for null you can always just call their methods. For data classes you can have something like String.Empty to represent a default value.

 

At work, for Java, which doesn't have explicit nullability (for non-primitive types), we use annotations like @NonNull/@Nullable to help us understand whether something is allowed to be null or not. Helps avoid errors. With languages like Kotlin you can't use null unless you explicitly mark something as nullable (e.g. String? instead of String).

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Null value isn't bad. It's just need to be used cautiously. Some people put null on field that logically cannot be null. Null is useful to specify field that are indeed unknown like for example a person might not want to disclose his age or birthday and those are 2 fields that make sense to allow to be null as unspecify. Although when working on queries you will need extra CPU work to do some work on those. Doing joining, grouping with null forces you to make mass replace of the value when doing so locking more row and using more RAM. Although the extra work is minor when you call enough of these queries it might forces you to add server resources. Also for each column that allow a null the row size on disk increase by 1 bit. This can add up fast. I have personally cleared database of nulls in the past and saved over 10 gb of data on disk.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×