473,570 Members | 3,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.G etById(doctor_i d)}
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.Offi ceNumber;
txtDoctorName.T ext = 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.NullDoct or)
{doctor = DoctorManager.G etById(doctor_i d)}
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 1694
SP

<an************ *@gmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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.G etById(doctor_i d)}
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.Offi ceNumber;
txtDoctorName.T ext = 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.NullDoct or)
{doctor = DoctorManager.G etById(doctor_i d)}
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
1879
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. The " & "" " on the right of each line should prevent nulls from being
3
1873
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. - affiliate_sub_id stores the affiliate ID of their affiliates. - response_code holds a number between 1 - 6.
3
2004
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 certain tasks have been completed. A typical example would be a column say invoice_value
6
28270
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 I'm no SQL expert, but if I'm summing (SUM) multiple fields and adding them together in my SELECT how does SUM handle Null? In some situations a...
0
1569
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, called Half, has a single integer input parameter and divides this value by two to produce its result. It also tests the input parameter to see if it...
1
2065
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 boat name in a text field OR a sail number in a text field, and gets the desired boat record back (an exact match).
6
2606
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 bigint;
0
282
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 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,...
6
5443
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 it--then hold the next value through the next set of nulls, and so on. See example below: I wanna copy down batch "IMR138" in record ID 1, all the way...
0
7732
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7638
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7956
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7703
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5523
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5247
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3671
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.