Tutorial :Why does .NET warn for 'Unused Variables' but not 'unused parameter'?



Question:

Why does .NET warn for 'Unused Variables' but not 'unused parameter'? (I believe Java warns in both cases.)

Why doesn't .NET care about 'unused parameters'?


Solution:1

This is not a technical answer, and I certainly don't know the technical answer if there is one. But, since you asked.

Unused variables are completely unattached. They have no anchors to anything and they do literally nothing except take up stack space. There is therefore no valid reason to have an unused variable in a complete program. You can declare variables as you are coding because you know you are going to need them later on, and you're just implementing part of a function now, but once your application is production ready, the unused variable do nothing except take up space.


There are many reasons why you may have/keep unused parameters. First and foremost, you would have to refactor every line of code which references that function. That is not necessarily easy or possible.

If you have a written an API, it becomes actually impossible to change all those lines of code. So in some cases you must leave the unused parameter in for API consistency.

Secondly, if you are adhering to or implementing an interface, then you do not have the option of removing the unused variable, it is forced upon you. Even if you don't require whatever information is in the variable, you must implement the correct call, and so you end up with unused parameter.

Thirdly, if you are creating a new API, and there is information which is required for many things, but not this particular call, it may be placed in there for consistency sake. Or if it is a feature that will be implemented in the next release, then you might as well get the call right in the first time, so that you next release does not become a breaking release.


Ultimately, the difference is that parameters are information which is given to you, so you may not always need to use it all, but variables are information you are saying you need immediately. Since local variables are immediate and do not affect anything out of scope, getting rid of unused variables is a lot easier and has no downside.


Solution:2

It seems to me like there are several reasons you might genuinely want to keep unused parameters; for example, suppose an application links to your DLL, and you make a change in your DLL -- to avoid recompiling the application, you may want to keep the function signatures the same.

I can't think of any similar reasons you would want to leave unused variables around and un-commented-out.


Solution:3

Good question! I assume that the Visual Studio team simply hasn't implemented that yet. I know that other code inspection tools, like ReSharper from JetBrains, will warn you about unused parameters.

Also, as another answer noted, there might be reasons for keeping unused parameters. In the case of a public method, the parameter might be used by another assembly.


Solution:4

It doesn't really matter as the compiler strips out unused local variables. The main reason I believe the compiler warns about local variables is because their presence without used is most likely due to oversight (either you declared a variable you didn't need or you refactored and an variable that was once needed now no long is needed).


Solution:5

You might be implementing an interface method or callback (delegate signature) with values that aren't needed for that particular implementation. For example, I never remember using the sender parameter in a standard event callback. This would be flagged all over the place.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »