Is String.Empty really so much more readible than ""?
More readable enough that it's worth the few extra keystrokes, yes. I
personally don't get the near obsession some programmers have with saving
a few keystrokes. As a percentage of all overall development, the time
it takes to actually type the code is small. Besides, in most cases, you
write the code once, but it's read dozens of time. So, even if you take
three extra seconds to type a more explicit reference (be it String.Empty
or anything else), you more than make up for the "investment" of the
single write during the multiple reads.
Is this a big deal? Not really. I happen to prefer String.Empty to ""
because I believe the former to be a bit more clear and that's enough for
me. What I find interesting, though, is that this discussion seems to
paralel in a way the arguments of C# vs. VB.NET. C# people love that
fact that they save a few keystrokes with their cryptic language. I
prefer to type a bit extra and have my code more easilly understood by
more people. Sure, if you know C#, then you know what weird combination
of punctuation marks you have to create to get the functionality you
need, but is this in-built obfuscation really worth the relatively small
savings in keystrokes? I prefer VB.NET, so obviously I don't think so.
To me, it's like driving ten miles out of your way to save a penny per
gallon on gas.
I'm not trying to start another C# vs. VB.NET holy flame war here. It's
just that the principle of "" vs String.Empty reminded me of some of the
same points, albeit on a much smaller scale.
- Mitchell S. Honnert
Phill W. said:
Herfried K. Wagner [MVP] wrote:
. . .
mystring = "" has been around for ages now an is obvious beyond any
doubt to me so I surely don't think saying mystring = string.empty is
any better.
Full ACK. I wonder why the VB compiler doesn't emit 'String.Empty' for
empty-string literals.
Wouldn't that be nice - from the little I understand of I.L.Code, the
compiler does come up with something a bit different in each case (not
surprisingly).
Given ...
If s1 = String.Empty Then
... the ILCode produced looks like:
IL_0001: ldloc.0
IL_0002: ldsfld string [mscorlib]System.String::Empty
IL_0007: ldc.i4.0
IL_0008: call int32 [Microsoft.VisualBasic]
...CompilerServices.StringType::StrCmp( string, string, bool )
But if you use the "old-fashioned" ...
If s1 = "" Then
... the compiler produces this instead:
IL_0001: ldloc.0
IL_0002: ldstr ""
IL_0007: ldc.i4.0
IL_0008: call int32 [Microsoft.VisualBasic]
...CompilerServices.StringType::StrCmp( string, string, bool )
So does it really matter?
Is "ldstr" so much slower than "ldsfld" that "String.Empty" really is
worth all the extra typing?
<advocate owner="devil" >
Is String.Empty really so much more readible than ""?
Regards,
Phill W.