Re: Initialising Variables



Keith Thompson said:

<snip>

Yes, but a default value (unless you can choose one that's guaranteed
to be invalid) can also mask a bug.

How so? Surely it will lead to incorrect results?

Suppose you initialize a variable with some default value, you intend
to compute a new value for it, but you fail to do so (that's the bug.
If the default value allows the code to execute but produce quietly
incorrect results, it's going to be difficult to track down the
problem.

I disagree. Your clue is that the results are incorrect. From there, it's
just a matter of finding out why. In my experience, this is a lot easier
when the results are reproducible.

If you had left off the initialization, your compiler
*might* have been able to warn you that you're using a possibly
uninitialized variable, or the random garbage in the variable *might*
have caused a more drastic failure during testing

Yes, but I see a lot of "might"s in there. If you could guarantee this
behaviour, I'd be a lot happier.

(as opposed to a
subtle failure that's still there in the shipped product).

If so, then that's a failure of one's testing process, since one's testers
clearly assume that any run not resulting in a crash is a successful run.
Well, testers, here's your final production code:

int main(void) { return 0; }

It doesn't crash. Hey, it must be right!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Re: use assert and defensive together
    ... It would be better if you used different return codes for each type of failure, but I'm not going to fix al of your code. ... because the calling program is notified and will report to the user that it failed and if the calling program is any good at all will report the reason for the failure. ... But by confusing programming errors with incorrect paths, you are bug hiding. ... The function is therefore incorrect because it should use them or incorrect because it takes them. ...
    (comp.lang.c)
  • Re: Most common two-word phrase uttered while programming?
    ... to crash, bug or give incorrect results. ... I've been playing NOLF way too much) ...
    (borland.public.delphi.non-technical)
  • Re: LDAP Lookup failure
    ... I thought that was incorrect as well, I think this must be incorrect logging ... I have failure auditing on on the DC policy, but no entries appear from the ... > Did you enable failure audits on the DC and check the logs? ...
    (microsoft.public.windows.server.active_directory)
  • Re: predict virtual memory use - elf headers
    ... possibly guessing if the call is inside a loop ... > (`execute it') seems to get people annoyed. ... and you'd never know that a failure had occurred because you'd ... not be able to determine that the value was incorrect. ...
    (comp.os.linux.development.system)
  • Re: Getting g_wszVBRQuality property
    ... The error code is just like if the parameter type is ... I don't think you can say it is a bug for g_wszVBRQuality because the call ... not that its type is incorrect. ... You can call MS Customer Support in your country if you think it's a bug. ...
    (microsoft.public.windowsmedia.sdk)