473,396 Members | 2,024 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,396 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 1678
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.