According to the frame you have given, it is hard to argue against those variables per se:
try {
SomeMethod();
} catch (SomeException e) {
// here e is unused, but during debug we can inspect exception details
DoSomethingAboutIt();
}
Take this for example. You are catching an exception and (perhaps) have code which deals with the exception in a proper way. The fact that you aren't using the actual instance of the exception does no harm, neither to the code flow nor to the understandability.
But what makes me think is when you write about the legitimation of »unused variables«
In fact, some of them even help in debugging
That is not the purpose of writing code: having to debug it later. In order to prevent misunderstandings, I have to say, that I am well aware, that oftentimes you have to fix your code; and sometimes debugging is the solution. But it should not be the first, what comes to your mind, when writing code, that you leave "anchor points", whose only purpose it is to make debugging easier.
SomeClass Func() {
// ...stuff...
var ret = FinalComputationResult(thing, foo, bar);
// ret is unused, but while debugging allows checking return value.
return ret;
}
Here it depends how complex stuff
is and how that is related to the value, which is returned.
As a "Golden Rule", I would say the following:
If it helps understanding the purpose of the code, it is okay to incease redundancy in your code
or to put it otherwise:
Write your code as redundancy free as is necessary to understand later easily, what the code is doing
That results in: Most of the time, you should remove "debug"-variables.
ret
is not unused.catch (SomeException e)
" - you are pretending there are other, similar cases. Enlighten me, which ones? I ask because in C# or C++, I have trouble to find a different situation for your case.