Initialize local vars to a known value...

  • Thread starter Thread starter Ray Cassick \(Home\)
  • Start date Start date
R

Ray Cassick \(Home\)

FxCop seems to hate this:

Public Class Foo

Dim mPrivate count As Integer = 0

..
..
..

End Class

Why? It says that the code is inefficient and not needed because the runtime
will do it for me.

Well heck, I want to be SURE it will be done, not rely on some runtime to do
it.

Sheesh.

--
Raymond R Cassick
CEO / CSA
Enterprocity Inc.
www.enterprocity.com
3380 Sheridan Drive, #143
Amherst, NY 14227
V: 716-316-5973
Blog: http://spaces.msn.com/members/rcassick/
 
Well heck, I want to be SURE it will be done, not rely on some runtime to do
It does not depend on the runtime. The language specification states
that also.
 
Ray said:
FxCop seems to hate this:

Public Class Foo

Dim mPrivate count As Integer = 0

.
.
.

End Class

Why? It says that the code is inefficient and not needed because the runtime
will do it for me.

Well heck, I want to be SURE it will be done, not rely on some runtime to do
it.

Sheesh.

So are you saying you don't trust the runtime to comply to its
specifications with respect to initialization?

Personally, I wouldn't use a runtime I didn't trust that far. Why are
you prepared to trust it in matters such as garbage collection if you
don't trust it to obey the specs in terms of initialization?

Jon
 
Jon Skeet said:
So are you saying you don't trust the runtime to comply to its
specifications with respect to initialization?

Personally, I wouldn't use a runtime I didn't trust that far. Why are
you prepared to trust it in matters such as garbage collection if you
don't trust it to obey the specs in terms of initialization?


And why trust FxCop over the runtime? :)

Heck, if you're really paranoid, run FxCop against the runtime and see what
you get...

Marc
http://nomagichere.blogspot.com
 
Marc Bernard said:
And why trust FxCop over the runtime? :)

If FxCop and the runtime disagreed, I'd trust the runtime. In this
case, FxCop is telling you *to* trust the runtime.
Heck, if you're really paranoid, run FxCop against the runtime and see what
you get...

Indeed :)

FxCop is really meant to give guidance rather than absolute rules - but
it's always worth at least seeing what it says...
 
Ok, ok.. maybe this is just me showing my age.

My whole life (while programming) I have always gotten into the habit of
clearly initiaializting my vars when they are created.

I guess this just threw me for a loop.
 
Ray Cassick (Home) said:
Ok, ok.. maybe this is just me showing my age.

My whole life (while programming) I have always gotten into the habit of
clearly initiaializting my vars when they are created.

And that's very valuable in a situation where the value is otherwise
*not* guaranteed. When a guarantee has been made though, it's fine to
use it :)

It's a similar situation to long-time C programmers still writing:

if (5==x)

in C#, despite that being less readable than

if (x==5)

On C, there's a benefit to the former version - it prevents an
accidental assignment. In C#, there's no benefit, and there's the
readability downside.
 
Back
Top