Welcome to day 21 of my 31 Days of ReSharper. Today, I’ll wrap up the topic of Alt+Enter.
There are also discretionary quick fixes — things that you can do to your code, but that you don’t have to. These are just things that people do a lot, so ReSharper automates them to make them a little easier.
The discretionary quick fixes are indicated with a yellow light bulb.
These can be a bit hard to find, because you have to have the cursor in the right place. That’s true of the quick fixes for errors and warnings, too, but you can see the places where those apply; they’re red or gray or squiggly; and you can jump to the next one with F12. With the discretionary quick-fixes, there’s no visual clue until you put the cursor in the right place and wait half a second or so, and there’s no keystroke to jump to the next one. You pretty much have to either (a) already know they exist, or (b) stumble onto them.
Which is why I’m covering a few of them, so you’ll know what to look for.
Removing unused braces
Suppose you have an
if block that used to contain multiple statements, so you had curly braces. And now you’ve changed the code so that there’s only one line of code, so you don’t want the curly braces anymore.
Where to put the cursor: At either the open or the close curly brace (either right before it, or right after).
If you position the cursor over a
public keyword, you’ll get a yellow light bulb. Press Alt+Enter, and you can change the visibility. It doesn’t save much typing, but occasionally it’s handy.
Where to put the cursor: Anywhere within the visibility keyword.
Split Variable and Assignment
If you’ve got a variable that’s being assigned as part of its declaration, and now you want that assignment to be conditional or some such, you can use Split Declaration and Assignment to quickly split the assignment into a separate statement.
Where to put the cursor: over either the variable name, or the ‘=’ sign.
Join Variable and Assignment
Reverse transformations are good. Join Declaration and Assignment moves the variable declaration to the line where the variable is first assigned.
Where to put the cursor: on the line where the variable is first assigned, over either the variable name or the ‘=’ sign.
Here’s what it looks like after the transformation. Note that it moved the declaration down to the assignment, rather than moving the assignment up to the declaration, so it’s not going to break any ordering dependencies in your code.
This is one of my favorites. It just reverses the condition on an
if statement, and swaps the “if” and “else” blocks. It’s purely a cosmetic change, with no effect on what the code actually does.
Where to put the cursor: on the
Sounds pretty predictable, and I won’t give a boring example. But it is a little bit smarter than just inverting the condition and the blocks. Here’s an example of why it’s my favorite:
This happens from time to time: an
if statement that spans the entire method. Here’s what happens after Invert If:
It realizes that the
if statement is the last thing in the method, so it generates a
return to bail out of the method early if there’s nothing to do. One less level of indentation on a large block of code, and one less line of code in the method. Cool!