Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Never use Nulls?

Tags:

c#

null

We are currently going through the long process of writing some coding standards for C#.

I've written a method recently with the signature

string GetUserSessionID(int UserID)

GetUserSession() returns null in the case that a session is not found for the user.

in my calling code... I say...

string sessionID = GetUserSessionID(1)
if (null == sessionID && userIsAllowedToGetSession)
{
    session = GetNewUserSession(1);
}

In a recent code review, the reviewer said "you should never return null from a method as it puts more work on the calling method to check for nulls."

Immediately I cried shenanigans, as if you return string.Empty you still have to perform some sort of check on the returned value.

if (string.Empty == sessionID)

However, thinking about this further I would never return null in the case of a Collection/Array/List. I would return an empty list.

The solution to this (I think) would be to refactor this in to 2 methods.

bool SessionExists(int userID);

and

string GetUserSessionID(int UserID);

This time, GetUserSessionID would throw a SessionNotFound exception (as it should not return null)

now the code would look like...

if(!SessionExists(1) && userIsAllowedToGetSession))
{
   session = GetNewUserSession(1);
}
else
{
   session = GetUserSessionID(1);
}

This now means that there are no nulls, but to me this seems a bit more complicated. This is also a very simple example and I was wondering how this would impact more complicated methods.

There is plenty of best-practice advise around about when to throw exceptions and how to handle them, but there seems to be less information regarding the use of null.

Does anyone else have any solid guidelines (or even better standards) regarding the use of nulls, and what does this mean for nullable types (should we be using them at all?)

Thanks in advance,

Chris.

=====

Thanks everyone! LOTS of very interesting discussion there.

I've given the answer to egaga as I like thier suggestion of Get vs Find as a coding guideline, but all were interesting answers.

like image 233
ChrisV Avatar asked May 14 '09 09:05

ChrisV


People also ask

Why we should not use NULL?

The fundamental problem of null is that it is trying to represent the fact that it is not a value while being assigned as a value. This fundamental flaw then snowballs and manifests into problems that we see in everyday production code.

Should I allow NULLs?

They are standards in the industry even with the widespread debate. If you decide to use NULLs, always use them uniformly across all of your database tables. Every table must allow them, and you should never use multiple placeholders in different tables. This can lead to bugs in your applications.

How do you not use NULL?

One way of avoiding returning null is using the Null Object pattern. Basically you return a special case object that implements the expected interface. Instead of returning null you can implement some kind of default behavior for the object. Returning a null object can be considered as returning a neutral value.

What does allow NULLs mean?

Allow Null means that when you change or insert the data you dont need to fill that data in. Its not compulsory.


1 Answers

nulls are definitely better, i.e., more honest, than "magic values". But they should not be returned when an error has happened - that's what exceptions are made for. When it comes to returning collections... better an empty collection than null, I agree.

like image 200
Erich Kitzmueller Avatar answered Oct 02 '22 01:10

Erich Kitzmueller