I see a lot of code that includes variables not used for anything:

The variable inside of that method does not add anything to improve the understanding of the code. That’s normally done to make debugging easier, when setting a breakpoint and hovering over the variable to inspect it:

That way, the developer can set a breakpoint, and hover over the variable to inspect it.

Instead of doing that, I’d rather have that method written like this:

When I need to debug that method and inspect what it returns, I use the shortcut to *introduce a variable*, do my debugging, and then either undo the change or hit the shortcut to *inline the variable* (most IDEs support such refactoring. Even if the one I’m using doesn’t, creating a variable named *x* or *temp* should take no time).

Some IDEs, such as Rider, even show a method’s return in the debugger without the need for a variable, as seen below:

Summing up:

  • Optimize the code for reading
  • Get familiar with your IDE’s debugging capabilities
  • Change the code to facilitate debugging when needed
  • Change the code back for optimized reading before committing it

Leave a Reply

Trending

Discover more from Claudio Lassala's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading