Peter Duniho
4/19/2008 4:00:00 AM
On Fri, 18 Apr 2008 20:00:23 -0700, Robert <ritterhaus@yahoo.com> wrote:=
> On Apr 18, 7:22 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
> wrote:
>
>> You didn't post a code sample to illustrate what you think is an =
>> erroneous
>> report of this error. However, I'll make the assumption that you've =
got
>> some code that you know for sure will get through at least one path t=
hat
>> will initialize a variable, but the compiler isn't noticing it.
>
> True. I said I had two instances of this error borrowed from an old C
> program. Here they are:
Well, interestingly neither of the examples you showed are really the be=
st =
way to do things in .NET anyway. We can refactor the code so that it no=
t =
only reads better (at least in my opinion), but it also avoids the CS016=
5 =
error. Specifically:
> while (true){
> try{
> input =3D int.Parse(System.Console.ReadLine());
> break;
> } catch {}
> }
Can instead be:
while(!int.TryParse(Console.ReadLine(), out input))
{
}
Because "out" parameters are required to be assigned before the method =
returns, that satisfies the assignment requirement for the variable =
"input".
And:
> if (year < thisYear) verb =3D "was";
> if (year =3D=3D thisYear) verb =3D "is";
> if (year > thisYear) verb =3D "will be";
Can instead be:
if (year < thisYear)
{
verb =3D "was";
}
else if (year =3D=3D thisYear)
{
verb =3D "is";
}
else
{
verb =3D "will be";
}
You should be using if/else if anyway, because if you satisfy one =
condition, you have no need to check the others. But beyond that, by =
providing a final conditionless "else", the compiler can tell for sure =
that after that section of code, the variable "verb" will be definitely =
=
assigned.
> [...]
>> For me, the inconvenience of having to assign variables isn't such a =
big
>> deal. My concern is that I think it's a very good idea for the compi=
ler
>> to treat unassigned variables as a bug, but forcing the programmer to=
>> assign variables to some default value that should never actually be =
=
>> used
>> winds up with the potential for obscuring an actual "unassigned =
>> variable"
>> bug.
>
> I think that that is my point exactly. In my source code, initialized
> variables imply some default value that may (or must) be used in
> absence of any other value.
I'm not sure how this is your point with the code examples given :), but=
I =
would agree that as a general issue it is in fact a problem with the =
current C# implementation (if not the specification).
> [...] The fact that the
> compiler refuses to compile working code disturbs me more than a
> little.
Well, in the end, it comes down to what's _provable_ by the compiler, =
given a specific set of rules. It's true that the rules could be more =
complicated than they are now, and thus would allow more true things to =
be =
provably true.
But it's a fundamental limitation of logic that not all true statements =
=
are provable. So eventually, we'd run into _some_ barrier anyway. :) =
=
The C# designers just chose to put that barrier a little closer to the =
trivial than you or I would have preferred.
And for what it's worth, the rules regarding definite assignment aren't =
=
actually all that trivial. On the one hand, I think that as complex as =
=
they are, I'm not really sure why they didn't go the extra mile to deal =
=
with this sort of thing. On the other hand, I can see the point of =
someone who feels that they are already complicated enough and that they=
=
shouldn't be made even more complicated.
> Agreed, and a compiler warning would be great. I for one actually read=
> all those warnings messages. Just my two cents.
Well, it _does_ need to be an error. The whole point is the be absolute=
ly =
sure that you're never using any variables that haven't been assigned. =
=
This is both a code correctness and data security issue, and it's a =
critical feature of C#.
So, we could argue in favor of more complicated definite assignment rule=
s, =
but we can't argue in favor of reducing lack of definite assignment to a=
=
warning. It has to be an error. The compiler will not let you execute =
=
code that it cannot prove to itself is safe (according to its own rules =
of =
"safe", of course...you can write code that will erase your hard drive, =
=
and the compiler's happy to let you :) ).
Pete