2 ms·
I don't accept the analysis that "uninitialized is better than a bogus initial value because we have a tool which can treat it as a trap value". Here is my mai
by kazinator 8y ago
I don't accept the analysis that "uninitialized is better than a bogus initial value because we have a tool which can treat it as a trap value".
Here is my main argument: once tool is used and discovers the uninitialized variable, of course, there will be a fix applied: either to initialize the variable, or to assign a value to it later in a conditionally executed path in order to prevent the uninitialized use in that path. Either way, when this fix is applied, the tool will thereupon stop detecting an uninitialized value in the scenario that is being tested. And the fix can be applied with a bogus value. In other words, our goal is to prevent the use of an uninitialized variable, and the only way to do that is to put a value in it before it is used, and that always carries the risk of a wrong value being put there.
Additional argument: we know that some initial value is wrong only because we have some test which reproduces the situation of that value being set up and used, and that test fails. If we don't have such a failing test, it doesn't help us to leave the variable uninitialized, if our goal is to establish a correct value for that situation for which that test is missing. The Valgrind tool will certainly inform us that the variable is uninitialized, but in reference to a different test. Our debugging effort will establish a correct value for that test, without any guarantee that the value is correct fro the untested situation.
Correctness is not achieved until all execution paths access a stable value of the variable. We can't magically make the jump from "uninitialized" to "correct initial value" with zero risk of "bogus value". The tool which detects uses of uninitialized data is silenced once we supply a value for that data; it doesn't help with the issue of choosing the correct value.
So basically the "don't initialize" argument is implicitly based on the belief that there is no way to detect a bogus initial value, except for the end user of the software to discover a malfunction in the field. And that, magically, even though we can't reproduce the consequences of a bad initial value in development, it helps us to instead leave the variable uninitialized and run everything under Valgrind; we will then be alerted to the uninitialized variable, and not make a mistake in choosing a value for it, even though we still don't have a test case which distinguishes bogus from non-bogus value.
In summary, it is not a credible proposition that the initial coding (with no test case) carries a risk of choosing a wrong initial value, whereas post-mortem fixing of an uninitialized variable bug (still with no test case) does not carry a risk of choosing a bogus initial value.