+ 1

Unnecessary assignment of value to 'definedVariable'

In Visual Studio, I have seen this message in a few of the exercises I have worked on.. Where the variable is initialized to 0 or empty...etc... and then I use it to have values assigned to that variable via a method. I see the variable grayed out on the initialization, with the following message "Unnecessary assignment of value to 'definedVariable'" It's suggested fix is to replace the variable with an underscore. eg. (only a sample) int x = 0; x = GetFactor(a); suggested change to int x = 0; _ = GetFactor(a); It doesn't throw a compiler error and it's not even really a warning... I do think it is kind of annoying... I don't change it because I want the clarity and I want to insure the variable is initialized with 0 or empty. vs having int x = GetFactor(a); Why is VS giving this suggestion? Is there a precedence that I am missing?

12th Jan 2020, 9:03 PM
laura
1 Respuesta
+ 3
Modern IDEs often recommend changes that reflect usual coding conventions, best practices and preferred coding styles, even if they have no impact on the result. Because they assume that someone else will read your code and these other coders expect the same consistency that is used everywhere. _ usually denotes a throwaway value that you don't need and don't use anywhere else. For example if you don't print x, then why do you assign it? I am not sure if your warning is related to something like this; anyway you can probably turn off these kind of warnings somehow if it bothers you :)
12th Jan 2020, 9:32 PM
Tibor Santa
Tibor Santa - avatar