You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In cases like this, the initial assignment of foo is unnecessary and arguably harmful.
If a variable is left unassigned, C# will analyze possible code paths and issue errors for any instances in which the variable is accessed before being definitely assigned.
Assigning an initial value circumvents this check and obscures developer intent. (Aside: I suspect that this practice is inspired by languages such as C++, in which a declaration without an assignment results in undefined behavior)
The proposed analyzer would flag the above snippet as an error. Similarly, any case in which a variable has two assignments without any possible accesses between them should be flagged as an error. (I'm uncertain of the practicality of searching for the latter case—this one may need to be punted)
Another common pattern is to have an initial assignment followed immediately by a check that sets the variable to a new value:
Although this pattern is sufficiently common that it may be more appropriate to warn instead of error for these.
Code fix nuances
For the first pattern, the code fix provider should offer to remove the initial assignment; this will need to be able to transform a var into an explicitly declared type.
There may be some edge cases related to ref and out parameters, especially when considering the pairs of assignments without intermediate access and early return statements.
Some initial assignments may have side effects (e.g. an assignment to the result of a method call). Code fix providers should not remove the method call when removing the additional assignment. Similarly, when assignments may have side effects, code fix providers should take care not to change the order in which statements are executed.
It would be better to provide no fix than to provide a fix that might change the behavior of the method.
The text was updated successfully, but these errors were encountered:
A pattern that I sometimes see in C# is:
In cases like this, the initial assignment of
foo
is unnecessary and arguably harmful.If a variable is left unassigned, C# will analyze possible code paths and issue errors for any instances in which the variable is accessed before being definitely assigned.
Assigning an initial value circumvents this check and obscures developer intent. (Aside: I suspect that this practice is inspired by languages such as C++, in which a declaration without an assignment results in undefined behavior)
The proposed analyzer would flag the above snippet as an error. Similarly, any case in which a variable has two assignments without any possible accesses between them should be flagged as an error. (I'm uncertain of the practicality of searching for the latter case—this one may need to be punted)
Another common pattern is to have an initial assignment followed immediately by a check that sets the variable to a new value:
These would be better written as
or
Although this pattern is sufficiently common that it may be more appropriate to warn instead of error for these.
Code fix nuances
For the first pattern, the code fix provider should offer to remove the initial assignment; this will need to be able to transform a
var
into an explicitly declared type.There may be some edge cases related to
ref
andout
parameters, especially when considering the pairs of assignments without intermediate access and earlyreturn
statements.Some initial assignments may have side effects (e.g. an assignment to the result of a method call). Code fix providers should not remove the method call when removing the additional assignment. Similarly, when assignments may have side effects, code fix providers should take care not to change the order in which statements are executed.
It would be better to provide no fix than to provide a fix that might change the behavior of the method.
The text was updated successfully, but these errors were encountered: