DSP2017

ReSharper – all you need is… refactoring & code templates! – part 2

Ladies and gentlemen – the second post about ReSharper! This time, let’s focus on refactoring, auto-generation of code and some smart ‘helpers’.

Assistance & helpers

  • Ctrl + D – duplicates the line where the cursor is.
  • Ctrl + Shift + F1 – opens small window with documentation of the class/method/type that the caret is ‘touching’. Below – documentation for ‘string’ type .
  • Ctrl + Shift + V – opens history of the things you copied to your clipboard. I’ve always wanted to have it! Just look at this:

 

Suggestions, refactoring and code auto-generating:

  • Alt + Enter – opens context menu with suggestions of changes in the line where caret is.
  • Ctrl + Shift + R – refactor suggestions, like the one below:
    My favorite options are:
  1. Introduce Field – opens the below window, so we can choose the way we want a field to be added.
    Let’s assume that we have a ‘starter code’ like this:
public static async void ImportClients()
{
      var client = new DataStoreProviderClient(DataStoreProviderClient.EndpointConfiguration.BasicHttpBinding_IDataStoreProvider);
}

Below I show you the changes in code that all the 3 options make.

Current member:

private static DataStoreProviderClient _dataStoreProviderClient;
public static async void ImportClients()
{
    _dataStoreProviderClient = new DataStoreProviderClient(DataStoreProviderClient.EndpointConfiguration.BasicHttpBinding_IDataStoreProvider);
    var client = _dataStoreProviderClient;
    //…
}

Field initializer:

private static DataStoreProviderClient _dataStoreProviderClient = new DataStoreProviderClient(DataStoreProviderClient.EndpointConfiguration.BasicHttpBinding_IDataStoreProvider);
public static async void ImportClients()
{
    var client = _dataStoreProviderClient;
    //…
}

Constructor(s):

private static DataStoreProviderClient _dataStoreProviderClient;
static CEIDGProvider()
{
    _dataStoreProviderClient = new DataStoreProviderClient(DataStoreProviderClient.EndpointConfiguration.BasicHttpBinding_IDataStoreProvider);
}

public static async void ImportClients()
{
    var client = _dataStoreProviderClient;
    //…
}

2. Introduce Variable – it will change the ‘starter code’ like this:

var dataStoreProviderClient = new DataStoreProviderClient(DataStoreProviderClient.EndpointConfiguration.BasicHttpBinding_IDataStoreProvider);

var client = dataStoreProviderClient;

3. Introduce Parameter – I love it! It not only adds a parameter to the method:

public static async void ImportClients(DataStoreProviderClient dataStoreProviderClient)
{
    var client = dataStoreProviderClient;
    //…
}

… but also puts appropriate values everywhere we use this method!

public IActionResult SomeControllerMethod()
{
    CEIDGProvider.ImportClients(new DataStoreProviderClient(DataStoreProviderClient.EndpointConfiguration.BasicHttpBinding_IDataStoreProvider));
    //…
}
  • Alt + Insert – pressed when caret is inside a class, interface etc declaration will open menu with some useful option (ex. adding constructor, properties that base on some existing variable, overriding members of base type and so on).

 

Code templates – my beloved!

  •  Ctrl + E + L – shows the below menu where you can choose the desired snippet… and then fill only necessary fields (use TAB to move caret from one highlighted text to the other):
  • Ctrl + E + U – typed over selected text will surround it with a construction like ‘if’, ‘while’ and so one… Like in the pic below:
Featured image Angelina Litvin.

Share this: