Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- From: "CMM" <cmm@xxxxxxxxxx>
- Date: Mon, 13 Feb 2006 18:34:50 -0500
helps simplifying code. BTW: I consider the rule "always assign an empty
string to string variables" lazy because it prevents the developer from
seeing potential problems.
I don't disagree.... my rule isn't to always initialize a string to Empty.
It's to always initialize it to something when I declare it. I try to
declare objects when I need them. You wouldn't see a bunch of Dim's that
don't initialize anything at the top my methods. That's VB.Classic
mentality. VB.NET's Dim is a whole new creature.
Anyhow, please describe "potential problems?" Like what? Give an example.
--
-C. Moya
www.cmoya.com
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@xxxxxx> wrote in message
news:OngtH9OMGHA.3276@xxxxxxxxxxxxxxxxxxxxxxx
"CMM" <cmm@xxxxxxxxxx> schrieb:
I won't trade correctness for simplicity. Assigning "" to strings by
default, just to avoid NullReferenceExceptions, feels like a hack that
will just hide the actual problem.
Using VB's type emptystring type coercion is more of a hack. The actual
problem is that a string should mean something unless you really need it
not to. You don't have to initialize it to "" or String.Empty. You can
initialize it when you NEED it as so: Dim s As String = SomeMethod().
What's wrong with that?
... AND even better.... declare it inside blocks (If, Loops, etc) so it
goes out of scope at the end of the block. Man, this is just programming
101.
If VB developer's are still putting dozens of Dim's at the top of their
methods, boy, that doesn't bode well for the future of this platform. Dim
in VB.NET is not the same ol' VB.Classic Dim. They're missing out on the
whole point.
Exceptions can be good things if
they help you find errors during development, suppressing them is
taking the easy way out.
Initializing string is not suppressing an exception. Using Len(str)
insetad of str.Length *IS* suppressing it.
I use 'Len' et al. to /intentionally/ suppress the exceptions if this is
semantically correct (samples given in previous posts).
Fact is, pretty much the entire programming world has settled on the
notion that this is good practice. In C++ it's practically a law. In C#
it's just a darn good practice. It's only in VB where developers feel
free to be super-lazy and write horribly and hard to maintain code if
they so wished where this seems to be tolerated.
In VB.NET people write 'If Len(s) > 0 Then' instead of 'if (s != null &&
s.Length > 0)' for example. I don't think this is a sign of laziness,
instead I consider it an important high-level programming feature which
helps simplifying code. BTW: I consider the rule "always assign an empty
string to string variables" lazy because it prevents the developer from
seeing potential problems.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
.
- Follow-Ups:
- Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- From: Herfried K. Wagner [MVP]
- Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- References:
- Is there a difference between passing "" and passing Nothing to a Windows API ?
- From: academic
- Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- From: m.posseth
- Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- From: Mattias Sjögren
- Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- From: m.posseth
- Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- From: Mattias Sjögren
- Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- From: CMM
- Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- From: Herfried K. Wagner [MVP]
- Is there a difference between passing "" and passing Nothing to a Windows API ?
- Prev by Date: File Access & The Novice Programmer
- Next by Date: Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- Previous by thread: Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- Next by thread: Re: Is there a difference between passing "" and passing Nothing to a Windows API ?
- Index(es):
Relevant Pages
|