Nulls and Nullable

by Administrator 26. September 2005 20:40

I recall reading a best practice somewhere that you should neither give nor expect null as the result of a method invocation.  This was absolutely the norm in C++ despite the somewhat common notion of the NullObject pattern (; the practice is still so common in .NET languages that I doubt many people are familiar with the notion of a null object.

Today I began self-reviewing my code for a mobile project I am in the process of wrapping up.  I found that this notion had made its way into some of my code and for the sake of consistancy I went about repairing my error.  The fact that I had things like

CustomerDataAccess dao = new CustomerDataAccess();
CustomerTruck ct = dao.FindByBarcode(bc);
if (null != ct) {
//Do something

in (shamefully) about half my code made me take a step back and question the value of what I was doing. First of all, I find it amusing that I still keep to the old C++ "Put your constants on the left hand hand side of any comparison" practice.  For those of you who never had the honor of programming C++ the following code would compile, but give you unexpected results:

if (lValue == 3){
// do something
if (lValue = 3) {

...since both "=" and "==" are valid inside an if statement.  To make a long story short, any successful action such as this will return a non-zero value and failure a zero value.  In C++ "if (0) " is valid syntax and therfore this could result in a hard to track down issue.  My question to my mentors was always "Well, if I can remember to use the const expression as the Lvalue why can't I just remember to do it right?"; but that's neither here nor there.

There are some examples of a NullObject pattern in the current .NET framework, for example using string.Empty instead of "" or null. 

Thinking further, many have accidently (mis)used this pattern for value-types in the current .NET language.  Have you ever used DateTime.MinValue in your code to signify "No value here" ?  In .NET 2.0 we will have nullable value types ( to make this a little bit cleaner for us. 

As I replaced code such as if (null != ct) with if( ct != CustomerTruck.None) it certainly did look cleaner and made me feel better about myself as a person but I wondered if it was worth going back over working code to do.  One additional benefit is that if I DO forget to check for null (or proper NullObject) in code somewhere, I will likely get some expected behavior rather than a NullReferenceException.  

My questions to the community are these: do you have your own argument for using a NullObject Pattern?  Do you use the NullObject pattern?

(Thanks to Terski for being my pre-post sounding board yet again)


CompactFramework 2.0 Presentation

by Administrator 14. September 2005 19:55

Due to the Visual Studio 2005 launch event in November my presentation of the Compact Framework version 2 to the Wisconsin .NET user's group has been pushed back to January 2006.  Considering how cool VS 2005 is I think its safe to say this launch event will be better than anything I could have possibly presented.

The CF 2.0 release is very near and dear to me due to the amount of pain I have experienced in the CompactFramework 1.0 and SqlCE 2.0, and conversely the joy I have found in porting one of my mobile apps to the new platform.  There are so many different things I could discuss related to the state of mobile development, I'd welcome anyone's ideas for specific things they'd like to see covered.  Some of the points I planned to touch on:

  • Important API differences
  • Important CLR differences
  • Important Visual Studio differences
  • SQL Mobile: this is a big one, especially from a performance perspective
  • COM interop on the CompactFramework
  • Data Sync processes
  • Discussion of Active Sync and Emulators
  • Discussions of Smartphone vs. Windows CE builds vs. WindowsMobile builds

I'd welcome any comments at ;see you in January.



by Administrator 6. September 2005 21:41

Next Tuesday the 13th I will be attending the .NET user group meeting, however it is also a birthday for me.  Being "young at heart", I still like my birthday.

The following Saturday, the 17th, will be an annual event, of epic proportions.  I have a good friend who's birthday follows mine by two weeks and for several years we have been having an Uber Party on the weekend in between.  If you have nothing better to do and would like to truly see me at my worst then feel free to join us at Hooligan's Super Bar on the East side for food, just look for me preaching about something to 20 or so uninterested rowdy people.  The yearly tradition ends with a trip to The Safe House where my friend and I drink two or three Mission Impossibles (Missions Impossible?) each.


About the author

Damon Payne is a Microsoft MVP specializing in Smart Client solution architecture. 

INETA Community Speakers Program

Month List

Page List

flickr photostream