Archive for March, 2008

ReSharper 3.1 vs Plain Visual Studio

Tuesday, March 25th, 2008

Personally, I’ve been a fan of comparison tables for ages, and now there’s one that hits the nail on the head!
 
Ladies and gentlemen, please welcome ReSharper 3.1 vs Plain Visual Studio - face to face, feature to feature!

Technorati tags: , ,

Create an overloaded delegate quickly

Sunday, March 23rd, 2008

Sometimes it is necessary to create method overloads that just pass default values to other overloads. ReSharper allows you to do this quickly and easily.

Take this simple method for example:

screenshot_01.jpg

We need to take the CreateUser method and add a new parameter to it. However, we also need to keep the old CreateUser method signature around for other code that still uses that version of the method signature. Using ReSharper’s Change Signiture (Control-F6) feature will allow us to create an overloaded method to solve our problem.

screenshot_02.jpg

When the Change Signature dialog opens for this method, we will go ahead and add our new email parameter to this method.

screenshot_03.jpg

In the above screenshot, notice the option “Delegate via overloading method” circled in red. Clicking this box is what will take the signature changes and turn them into a new overloaded method.

When the changes are ready, clicking Next in the Change Signature dialog will now produce the following code.

screenshot_04.jpg

Notice that in the SaveNewUser method, the original method signature remains intact, but it is now delegated through an overloaded method. The CreateUser method now has two overloaded variations, the delegating method and the main method, where we can now add code to handle the new email parameter.

Jeff Pennal
JetBrains .NET Evangelist

Custom Templates

Saturday, March 15th, 2008

ReSharper templates enable you to quickly generate predefined code constructs. Many templates are provided ready to use; you can create new templates, edit existing ones and easily manage them.

Creating and using your own templates is a quick and easy way to help eliminate repetitive code or to automate common tasks. To demonstrate this functionality, I am going to show how to use custom templates to add log entries using the Apache log4net library.
(more…)

ReSharper in Detail: Quick-Fixes vs Context Actions

Tuesday, March 4th, 2008

Of all ReSharper’s features, two are most noticeable: context actions and quick fixes. Along with marker bar, these are exactly what a user sees every time he works with a real-world solution.
 
It’s pretty obvious what these features do - both offer drop-down lists filled with various available actions for modifying your code. However, specific differences between context actions and quick-fixes are not readily evident. Bearing that in mind, we will compare the two features below.
 
Availability of quick-fixes and context actions for a code position is designated by two differently colored bulb icons to the left of current line:

  • red for quick-fixes, and
  • yellow for context actions.

Whenever both quick-fixes and context actions are available at a specific caret position, a red bulb is displayed.
 
When any of two bulbs is displayed, pressing Alt+Enter launches a drop-down list with all available quick-fixes and/or context actions. If only quick-fixes are available, the list looks like this:


 
If only context actions are provided:


 
If both quick-fixes and context actions are available:


 
Quick-fixes are specifically designed to fix any code with highlighed errors, warnings, suggestions, or unused elements. In other words, wherever you see a code item marked with a red, green or blue curly underline, or a symbol highlighted in grey, you can bet there’s a quick-fix provided. Applying a quick-fix at a specific line can cause code modification in any other corner of your solution. For example, the “Create Method” quick-fix applied upon a call to a nonexistent method causes ReSharper to create the corresponding method after the current method ends or in another class:
 
Before applying:

 
After applying:

 
Another example would be triggering a quick-fix over a method call to modify the type of an argument in the corresponding method declaration, which can possibly reside in another file:
 
Before applying:
(Method call)

(Method declaration)

 
After applying:
(Method call)

(Method declaration)

 
On the contrary, context actions serve to provide ways to change your code instantly. They’re not associated with any discovered errors but displayed merely because they might be useful to you. After all, if you’re looking to surround a string with a string.Format() call or merge two nested if statements, it’s much easier to do with context actions than by hand.
 
Since quick-fixes are tied to the results of code analysis, they’re only applied to C# code. As for context actions, three separate sets of them are available for C#, VB.NET and XML.
 
For both C# and VB.NET, you can explicitly choose which context actions to enable. That can be done using language-specific Context Actions tabs in ReSharper Options. No comprehensive list of quick-fixes is available to a user and you can’t disable quick-fixes associated with compilation errors one by one - however, you can abandon code analysis altogether (to do that, clear Enable code analysis in ReSharper | Options | Code Inspection | Settings). You can also fine-tune the list of displayed warnings and suggestions that have quick-fixes associated with them (ReSharper | Options | Code Inspection | Inspection Severity).
 
For examples of using context actions and quick-fixes in practice, see the following links:

Technorati tags: , , , , , , , ,