Optimize code for reading, not debugging

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
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: