String Init best practice

  • Thread starter Thread starter Alvin Bruney [MVP]
  • Start date Start date
A

Alvin Bruney [MVP]

Sorry if this appears twice. Our mailserver died with that virus.

I see advice on this newsgroup about initializing strings to null. I
believe it is more prudent code to initialize a string variable to
string.empty instead of null. this leads to more robust code because a
programmer may try to touch this variable without testing it leading to a
null exception. Consider:

string str = null;
[snip]
str.length + 1;// blows here

string str = string.empty
[snip]
str.length + 1;//safe

Yes I know this is wide open for debate, i'm not looking for a fight
either.
I'm simply looking to test my theory to see if it holds up hence the post.
I
think programmers should always be on the defensive. ???
 
In theory, I like the null initialization better b/c strings are immutable.
On the other hand, if someone happens to reference length like you show,
then an exception is thrown. Exceptions aren't cheap, so this may offset any
efficiency realized by leaving the string null and only changing it once
when you need it.

To get around this problem, I try as much as possible, to hold off
declaration until I"m going to set it to an actual value. Occassionally
this can be a pain within try catch blocks for example, but as a general
guideline , it works pretty well.

Alvin Bruney said:
Sorry if this appears twice. Our mailserver died with that virus.

I see advice on this newsgroup about initializing strings to null. I
believe it is more prudent code to initialize a string variable to
string.empty instead of null. this leads to more robust code because a
programmer may try to touch this variable without testing it leading to a
null exception. Consider:

string str = null;
[snip]
str.length + 1;// blows here

string str = string.empty
[snip]
str.length + 1;//safe

Yes I know this is wide open for debate, i'm not looking for a fight
either.
I'm simply looking to test my theory to see if it holds up hence the post.
I
think programmers should always be on the defensive. ???

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
 
Alvin,
I see advice on this newsgroup about initializing strings to null. I
believe it is more prudent code to initialize a string variable to
string.empty instead of null.

Personally I avoid to initialize it at all until I have something
useful to put in it if at all possible. That way the compiler will
tell me that the variable hasn't been initialized if I try to use it
too early. When it isn't possible to avoid, I usually pick null as the
initial value.

this leads to more robust code because a
programmer may try to touch this variable without testing it leading to a
null exception.

If the programmer is playing with things that haven't been properly
initialized, I'd consider it a bug. Getting an exception for that is a
good thing, sinec it hopefully helps you locate and fix the issue.



Mattias
 
Sorry if this appears twice. Our mailserver died with that virus.

I see advice on this newsgroup about initializing strings to null. I
believe it is more prudent code to initialize a string variable to
string.empty instead of null. this leads to more robust code because a
programmer may try to touch this variable without testing it leading to a
null exception. Consider:

string str = null;
[snip]
str.length + 1;// blows here

string str = string.empty
[snip]
str.length + 1;//safe

Yes I know this is wide open for debate, i'm not looking for a fight
either. I'm simply looking to test my theory to see if it holds up hence
the post. I think programmers should always be on the defensive. ???

It depends. If your string variable isn't meant to be used before it's
been assigned another value, then setting it to null is actually better
- precisely because it means you'll get an exception if you try to use
it, and so are likely to fix the bug earlier. Using an empty string
instead, your code will continue but with dodgy data - which in my view
is a bad thing.
 
Back
Top