Returning nulls

A

anonymous.user0

Whats the common/best practice for returning a "Does not exist/not
found" result from a function. Imagine I've got a function:

myObjectType GetById(long id);

where the object may or may not be found. Currently, I just return a
null. But this muddies up the calling code with null checks and stuff.
Example:
I've got an Appoinment class, that has a property Doctor of class
Doctor. This property is lazily loaded, so I only store the id of the
doctor in the Appointment class (doctor_id).

public property Doctor Doctor{
get{
if(doctor == null){doctor =
DoctorManager.GetById(doctor_id)}
return doctor;
}
set{
doctor = value;
if(value != null){doctor_id = value.id;}
else{doctor_id = 0;}
}
}

Now, when using the Doctor property in Appointment class, I always have
to check if the Doctor is null. This can be ugly when doing something
like displaying the Appointment information.

private void showAppointment(Appointment apt){
txtTime.Text = ...
if(apt.Doctor != null){
txtRoom.Text = apt.Doctor.OfficeNumber;
txtDoctorName.Text = apt.Doctor.Name;
}
etc...
}

I was thinking of creating a static Doctor instance in the Doctor class
that represents a Null Doctor like this:
public class Doctor{
public static readonly Doctor NullDoctor = new Doctor("Null
Doctor name", "Null Doctor Office",...);
...
}

and in the GetById() functions, returning the NullDoctor if none is
found. This of course, would cause me to change my Doctor Property to
something like this:
public property Doctor Doctor{
get{
if(doctor == null || doctor = Doctor.NullDoctor)
{doctor = DoctorManager.GetById(doctor_id)}
return doctor;
}
set{
doctor = value;
if(value != null){doctor_id = value.id;}
else{doctor_id = 0;}
}
}

Pros/Cons for each approch?
 
S

SP

Whats the common/best practice for returning a "Does not exist/not
found" result from a function. Imagine I've got a function:

myObjectType GetById(long id);

where the object may or may not be found. Currently, I just return a
null. But this muddies up the calling code with null checks and stuff.
Example:
I've got an Appoinment class, that has a property Doctor of class
Doctor. This property is lazily loaded, so I only store the id of the
doctor in the Appointment class (doctor_id).

public property Doctor Doctor{
get{
if(doctor == null){doctor =
DoctorManager.GetById(doctor_id)}
return doctor;
}
set{
doctor = value;
if(value != null){doctor_id = value.id;}
else{doctor_id = 0;}
}
}

Now, when using the Doctor property in Appointment class, I always have
to check if the Doctor is null. This can be ugly when doing something
like displaying the Appointment information.

private void showAppointment(Appointment apt){
txtTime.Text = ...
if(apt.Doctor != null){
txtRoom.Text = apt.Doctor.OfficeNumber;
txtDoctorName.Text = apt.Doctor.Name;
}
etc...
}

I was thinking of creating a static Doctor instance in the Doctor class
that represents a Null Doctor like this:
public class Doctor{
public static readonly Doctor NullDoctor = new Doctor("Null
Doctor name", "Null Doctor Office",...);
...
}

and in the GetById() functions, returning the NullDoctor if none is
found. This of course, would cause me to change my Doctor Property to
something like this:
public property Doctor Doctor{
get{
if(doctor == null || doctor = Doctor.NullDoctor)
{doctor = DoctorManager.GetById(doctor_id)}
return doctor;
}
set{
doctor = value;
if(value != null){doctor_id = value.id;}
else{doctor_id = 0;}
}
}

Null Object is a common pattern. It encapsulates the behavior expected when
there is no object and eliminates special null handling code that is
scattered throughout your software. If implemented you would eliminate the
== null checks and either check the IsNull property or do no checking at
all. This is because the null object should act correctly, e.g. it's ID
would be 0 as that is the behavior you have decided you want with the
current "null". You will still use the == null checking for your lazy
loading as it is an indicator that no object has been created. However the
object that is created may be a real Doctor or a Null Doctor (there is no
Doctor with that ID). Therefore == null and myDoctor.IsNull are not the same
and mean very different things.

Refactoring by Martin Fowler has a few pages on this.

SP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top