473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Returning nulls

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?

Feb 16 '06 #1
1 1676
SP

<an*************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Feb 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dan Perlman | last post by:
From: "Dan Perlman" <dan@dpci.NOSPAM.us> Subject: ODBC creating nulls? Date: Friday, July 09, 2004 10:43 AM Hi, Below is my VB6 code that writes data from an Access 2000 table to a PG table....
3
by: adam | last post by:
Hello, I have a MySQL table that has (among other fields) an affiliate_id, affiliate_sub_id, and a response_code field. - affiliate_id stores the unique number of each of my affiliates. -...
3
by: aaj | last post by:
Hi I am probably going to regret asking this because I'm sure you are going to tell me my design is bad 8-) ah well we all have to learn.... anyway I often use Nulls as a marker to see if...
6
by: mike | last post by:
I'm doing what I thought was a simple GROUP BY summary of fairly simple data and the my numbers aren't working out Some results are showing up <NULL> when I know the data is in the database ...
0
by: Rhino | last post by:
I am working with SQL Functions in DB2 for Windows/Linux/UNIX (V8.2.1) and am having a problem setting input parameters for SQL Functions to null in the Development Center. My simple function,...
1
by: PST | last post by:
Here's a problem I'm trying to deal with: I'm working on a Frontpage 2000 website for a boat handicapping system, built in Access 97. What I'm trying to accomplish is: The user enters a...
6
by: Josh Close | last post by:
I'm having a problem with a value coming out of a loop. CREATE OR REPLACE FUNCTION funmessagespermintotal() RETURNS int8 AS ' DECLARE this_rServer record; this_rSum record; this_iSum...
0
by: anonymous.user0 | last post by:
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...
6
by: Cliff72 | last post by:
I need to fill in the nulls in the batch field the value from the record immediately preceding the null one ie replace the nulls with the preceding value until I hit a record with a value in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.