Fog Creek Software
Discussion Board

NULL pointers vs. empty strings--preference?

If a C text processing function encounters empty or missing fields/values/sets, how do you prefer it handle them? Namely, if it returns a struct comprised of the items it extracts, do you prefer the empty or missing parts to be stored in the struct as NULL pointers or empty strings?

One Bowl. One Hundred Percent.
Thursday, March 25, 2004

(By "empty strings" a mean a pointer to an empty string, of course)

Thursday, March 25, 2004

If there is a difference between a string being zero-length and being completely absent, then I'd use NULL. Otherwise I'd use (char*) "";

Dan Maas
Thursday, March 25, 2004

For me it really depends on the situation.  Sometimes there is no difference between a NULL string and an empty string.  For example, if I'm calling some function that returns a string, and I plan on combining a bunch of these results together:

for (int nLoop = 0; nLoop < nLength; nLoop++)
    myString += someFunction(inputString, nLoop);

Here, I probably don't want to receive a NULL pointer back, but would rather just receive an empty string and continue on with the results.  On the other hand, if I want to differentiate between an empty string and nothing at all (i.e. maybe a record from a DB), I definately want to return NULL indicating that there is no record, or "" if there is a record, but it contains no information in it.

Thursday, March 25, 2004

The geniuses here use a single space to represent no value in both the database and code (Perl).

Thursday, March 25, 2004

They probably didn't invent that themselves; I've seen it somewhere else.

Illustra maybe? Can't remember.

Joel Spolsky
Fog Creek Software
Thursday, March 25, 2004

Perhaps they had to work against Oracle? Apparently in Oracle an empty string is null (i.e. violates NOT NULL fields rules). Using a single space "fixes" that - although we decided to go the more "proper" route and use NULL instead (our app doesn't rely on empty strings)

Chris Ormerod
Thursday, March 25, 2004

It was Oracle - ORCL never implemented NULL's right,
so NULL's and empties satisfy IS NULL. 

Postgres/Illustra did this sort of thing correctly - an empty
string IS NOT NULL.

Thursday, March 25, 2004

It is rather strange that someone would use a space for representing NULL in Perl when there is an obvious and standard way of doing it -- the undef value. It translates automatically to a NULL when using DBI.

Maybe the space is something that was the easiest solution on some other platform?

Johan Lindström
Monday, May 24, 2004

*  Recent Topics

*  Fog Creek Home