Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it wrong to use "extra" variables because it's easier for debugging?

Tags:

c#

I was just debugging some code that looked like this:

string someValue = _snuh.FindItem(id).Value;

I wanted to inspect the return value of FindItem() (it returns a Foo), so I broke the code into two lines:

Foo foo = _snuh.FindItem(id);
string someValue = foo.Value;

This allowed me to look at foo in the debugger; something I wasn't able to do when the code was all on one line.

Now that I'm done debugging, should I put the code back the way it was, or leave it as two lines?

like image 206
Bob Horn Avatar asked Nov 30 '22 04:11

Bob Horn


1 Answers

The two lines are better than the one liner:

  • You can debug them
  • If you have a null pointer, you will see the line number in your error logs (and see directly what is wrong)
  • It is more readable
  • No need for extra comments to explain what the one-liner is doing
  • There is no difference in performance (the compiler will optimize to the same IL)
  • If you rewrite code after debugging it, you can introduce new typos
like image 63
Peter Avatar answered Dec 05 '22 05:12

Peter