Tough question on database NULLs.

  • Thread starter Thread starter Karen Hill
  • Start date Start date
Mark said:
Do you also recommend using Decimal.Zero?

Of course - and when at some point down the line the value of zero
changes, all those fools who embedded magic numbers in their code will
be sorry! :)
 
Jon Skeet said:
And to get back to Mark's point, if you use "" everywhere in your code,
that will still only be one object - so using String.Empty wouldn't buy
you anything.

It would. If you don't like seeing literals in your code (in-line) then it
would buy you the satisfaction of using a non-literal without defining your
own constants :P

Mythran
 
I've seen people using something very similar, with a few small changes:

public static void SetColumnValue(DataRow Row, DataColumn Column, string
value)
{
if (value == string.Empty &&
Column.AllowDBNull) {
Row[Column] = DBNull.Value;
}
else
{
Row[Column] = value;
}
}

This makes assignments even better, or at least shorter :)

SetColumnValue(Row, Row.TypedTable.LastNameColumn, txtLastName.Text);

I've also seen customers with a little change in the Enterprise Library
that does this check in the low level storage code. There's even a line in
the OracleDatabase class which is commented out, but does this by default.
This is due to the fact that Oracle handles strings differently by
default.

Jesse

Yeah, on the UI side..but we do it on the BLL side because you may not have
complete control over what is calling the BLL (such as multiple-UI's,
web-services, etc.). Massaging/Validating the data in the BLL is the BLL's
job (it is business logic to massage or validate data anyways).

Mythran
 
Jon Skeet said:
It would. If you don't like seeing literals in your code (in-line) then it
would buy you the satisfaction of using a non-literal without defining your
own constants :P

Mythran

Oh, dear. That brings back bad memories from my C years. What C
programmer didn't run across nonsense like this while doing
maintenance:

if (items_remaining == ONE)
{
cost += TWO_POINT_FIVE;
}
else
{
cost += POINT_FIVE;
}

etc.

No, I'm not joking. Not only did I see crap like this, but the guy who
wrote it fiercly defended it, claiming that his code was more readable
because it contained "no literals". Ugh. I wonder whether String.Empty
isn't just more of the same.
 
Oh, dear. That brings back bad memories from my C years. What C
programmer didn't run across nonsense like this while doing
maintenance:

if (items_remaining == ONE)
{
cost += TWO_POINT_FIVE;
}
else
{
cost += POINT_FIVE;
}

etc.

No, I'm not joking. Not only did I see crap like this, but the guy who
wrote it fiercly defended it, claiming that his code was more readable
because it contained "no literals". Ugh. I wonder whether String.Empty
isn't just more of the same.

Oh yeah, that's where I got it from :) Ole C/C++ days :)

and if I wrote the a similar sample you wrote above, you can shoot me ...

Another reply in this thread makes more sense than what I wrote...using
string.Empty is a whole lot easier to see and comprehend than "" and "'".
You can easily mistake "'" for "" and "" for "'" when skimming through code
and trying to track down errors. So, using string.Empty for "" and "'" or
''' for the apostrophe character, does help ... a little ;)

Now, there are times to use literals in code...when you KNOW that the value
will NEVER be different ... even for testing purposes. I'm not one to
defend it with all my heart though, as I use literals in my code ... but I
try not too.

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top