CMM said:
Sometimes you have no idea if the procedure you're calling cares or
not. Instead of checking for a null string with a myriad of If/Else
loops all over the place.... why not just do the decent thing and
initialize your string???
Sorry, but you don't get the point. Why do you *always* ("always" means: no
matter which code path the CPU will go in this procedure) initialize it with
""?
- If it will be "" always, use a constant.
- The other case is "not always". "Not always" means, you have different
cases. If you have different cases, you can set it to "" in one case and to
other values in other cases. You will *never* run into a
NullReferenceException after the If-Then block. If you do, you forgot the
Else block. In addition, why always set it to "" if it will be overweritten
in one of the cases anyway?
This is true for both, when using the variable in your own procedure as well
as when passing it to another procedure.
I've written pretty much VB.Net code meanwhile but never had to initialize a
string variable to "" ever.
Doh! No intellisense in a newsreader window. Good eyes! But, you
know what I meant by the code.
And my code doesn't look like that.... it always looks like:
Dim s As String = String.Empty
Dim a As String = String.Empty
And, I also initialize arrays for similar reasons...
Dim ary As Integer() = {}
I highly recommend it.... and so does Visual Studio's Code Analyzer.
Makes for nice bug-free, easy-to-read code.
Initializing arrays this way doesn't make sense as well. If you will always
have an empty array, you don't really need one. If it will not
always be empty, you can create an empty array in one case and a
not-empty array in other cases.
Of course, for both (strings and arrays) there are exceptions, but *as a
rule* what you're doing doesn't make sense at all.
Armin