Hadi Hariri's Blog

JetBrains TV: It’s here…

Thursday, 2 September 2010 10:59 by hadi

For some time now, we’ve had requests from the community to have a central point where people could have access to Screencasts and other Video related material that we provide. In response to that, we are happy to announce the availability of JetBrainsTV (Beta!).

 

image

 

The content is organized by Channels, currently one per product, although we will be expanding that to include other topics in the near future. Most of this content, if not all, is tagged with keywords, allowing you to easily locate topics you are interested in. In addition, the cross-cutting nature of the tags allow you to discover new things, for instance functionality that you knew existed in IntelliJ but weren’t aware that ReSharper also provided.

Content can be voted and commented on, neither of which require you to create an account. Voting is anonymous and comments can be left as a Guest user or using other social media accounts such as Twitter, Disqus or OpenId (obviously based on level of Spam we might have to adjust this in the future).

 

Contributing content

JetBrains TV is not only a platform to offer centralized screencasts, but also a chance for community members to contribute. By signing in (click on the link in the top right-hand corner), you can upload your own videos, thus providing you with a platform for other community members to see your work.

The sign-up process is easy, in fact, you might not even need to create an account, as it uses the consolidated JetBrains account. If you’ve contributed to the forums or the JetBrains developer  
community in the past, you’re good to go!

Although it is still in Beta, there’s quite a lot of content on there already and we hope that by opening it up to the community early, we can improve things based on your valuable feedback.

Enjoy!

 

PS. To support our love for dogfooding we’ve implemented the TV site with PHP and Drupal using PhpStorm IDE. PhpStorm team has received a good bunch of feedback they wanted.

Tags:   ,
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Templates Galore: Surround Templates

Wednesday, 1 September 2010 16:26 by hadi

This is part three of a four part series on Templates:

Surround templates, as the name indicates, allow us to surround a selection of text and apply a transformation to it. This can be from wrapping some code inside a try..catch block or performing more complex tasks such as iterations.

Surround Template Gallery

As with Live Templates and File Templates, we can view the list of available Surround Templates via the Live Templates entry in the ReSharper menu (for the record, I’ve filed a request for it to be renamed to the more appropriate Template Explorer, so feel free to support my request by voting for it).

SNAGHTML5131d3fb

The layout is similar to File Templates. The right-hand side column allows us to define a “quick list” of Surround Templates we want available based on context. Like before, we can first see how to use existing ones and then expand them by adding new ones.

Invoking a Surround Template

Like pretty much everything in ReSharper, we can invoke Surround Templates using the ReSharper menu (ReSharper|Edit|Surround with Template) or alternatively and more conveniently via the shortcut, which is Ctrl+E,U if using Visual Studio Scheme (Ctrl+Alt+J for IntelliJ).

Let’s assume we have some code that we need to wrap in a try..catch block. We first select the line(s) of code, press Cltr+E,U and ReSharper prompts a menu for us to select the specific template. Each entry is numbered, allowing us to quickly make a selection (in this case it would be 8)

image

The selected part of the code is then wrapped inside the try..catch part (later on we’ll see how this is defined), resulting in:

image

This is not limited to C# code, but can be used in Visual Basic as well as HTML (ASPX) and XML pages. Take the following text in an ASPX page:

image

Invoking the Surround Templates provides us with the option to surround it with a tag, link or other constructs. In this particular case, if we select a tag, we’ll be presented with a second menu which allows us to define the actual tag. ReSharper provides a list of all tags currently on the page, but we are free to start typing our own:

image

as shown here (writing span):

image

or when iterating over a series of fields, we can invoke the foreach template:

image 

resulting in:

image

 

Creating new Surround Templates

Below is a typical MVC Action. We receive a customer and save it to a repository

image

The data however might not always be valid. That’s why it’s wise to validate it before operating on the data. This is usually done in ASP.NET MVC checking the IsValid property of ModelState:

image

Let’s see how we can easily make a Surround Template to do this for us, that is go from the single _customerRepository.Save(customer) to the code above*.

1. Open the Surround Template tab using ReSharper | Live Templates | Surround Templates

2. Create a new User Template, by right-clicking on User Template and selecting New Template

image

3. Enter the follow text in the Template Editor, giving it the name ModelState:

image

With Live Templates and File Templates, we generate text from scratch. With Surround Templates, we mostly select existing text and our intention is to work with it. As such, ReSharper offers a series of pre-defined variables that helps us with this task:

Variable Description
$SELECTION$ The selected text when invoking the template
$END$ Where the cursor should be placed on termination of template interaction
$SELSTART$ Denotes the start of selected text after template has been invoked
$SELEND$ Denotes end of selected text after template has been invoked

The new items here are $SELECTION$, $SELSTART$ and $SELEND$, which we’ll see their purpose shortly. As always, anything else in between the $ will act as a variable on which we can apply a macro.

4. Similar to File Templates, in order to make this efficient, we need to add it to the quick list of templates. Drag the newly created template. You can optionally set the filter to C# files only. See previous entry on Live Templates to see how to do that

 

image

5. Close the Template Editor.

6. Select the line of text we want wrapped in the template and press Ctrl+E,U. Choose ModelState

image

We should now end up with the following:

image

Refining the template

As it is now, the Surround Template created assumes that we always want to perform a RedirectToAction, which is the norm**. If however on occasions we want to perform some other operation, we can make it easier to replace the default RedirectToAction call by selecting the text. As such, all we need to do is start typing to delete it. We can do this using the $SELSTART$ and $SELEND$ variables as shown below:

image

This is now telling ReSharper to prompt for the variables, but to ultimately select the entire RedirectToAction for us to replace it if required. If we are OK with this default, we can hit ESC. We also change the order in which the variables are prompted for to make it more convenient.

Summary

Surround Templates offer us a way to conveniently add construct around existing code with ease. They are different than Live and File templates in that the concept of a selection exists. With Surround Templates, we cover the three different types of Templates that exist in ReSharper. In the last part of this four part series we’ll see how we can create new macros to extend the ones ReSharper offers us when playing with variables. This will allow us to do all sorts of things with templates.

 

Notes

* I’ve used this as an example because it’s all too common to see this approach. Personally however, I recommended using AOP to avoid constant repetitive code duplication across actions.

** This is known as the PRG pattern: POST-Redirect-GET. The reason we do this and not return a View is because if the user presses the Refresh button in the browser, it would act as a new POST. In this case maybe nothing will occur, but think about adding new records for instance. By first performing a Redirect and then doing a GET for Index, a refresh will only cause a new GET of the request we redirected to (in this case Index action).

Tags:  
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Consistency: Your best friend or your worst nightmare

Monday, 30 August 2010 14:37 by hadi

Consistency: We developers love it don’t we?

We come up with beautiful architectures making sure everything is consistent. If we use a service in one place, we’ll use it everywhere. If we have a ViewModel for showing data, we’ll use a ViewModel everywhere, even if it’s not needed. Because we need to be consistent.

Partly I guess this is motivated because we think that being consistent helps ourselves and teammates understand what’s going on, even if that drives us to YAGNI and a series of WTF’s of why we did something that’s not required. We do it to be consistent!

It might seem inoffensive, but consistency can be damaging. It leads us into trying to homogenize everything despite it not being the appropriate approach in many cases.

So next time someone asks:

image

…it’s cause we like to be consistent!

Breaking consistency when required not only lowers levels of WTF’s, it can even help scalability!

Tags:  
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (5) | Comment RSSRSS comment feed

Regression tests, do it now.

Monday, 30 August 2010 13:05 by Hadi

When you encounter a bug, do you first write a failing test before fixing it? You should. But the vast majority of us sometimes do not. External pressure (read Customer, Teammates or Management) move us in the opposite direction. Everything we’ve learnt about unit testing and good software practices go out the window, and the sheer pressure forces us to fix the issue as fast as possible! We focus more on getting the job done* and deploying than worrying about adding another test to our suite.

Adding that test is not about increasing our code coverage or patting ourselves on the back, with an optional tweet, that we are good developers. It’s about putting regression tests in place.

Of course, we could always just apply the fix, and then later, calmly add a test. However, there’s always another urgent bug fix, feature, or customer breathing down our necks, so things just get putt off. 

It’s hard to not be tempted to just fix something then and there. Fight the temptation though if you can. Write that failing test first!

[*Implicitly changing the definition of Done on the go]

Tags:   ,
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (2) | Comment RSSRSS comment feed

Templates Galore: File Templates

Tuesday, 24 August 2010 15:33 by hadi

Continuing with Templates, in this post we’re going to take a look at File Templates. Whereas Live Templates allow us to generate interactive code and insert this at the current cursor position, File Templates allow us to do the same but directing the output to a file. Additionally, as we’ll see, they are very useful for creating both standard and custom files efficiently.

If we open up the Templates Explorer via ReSharper | Live Templates, we can see three tabs: Live Templates, Surround Templates and File Templates.

SNAGHTML2cd01427

Similar to Live Templates, Files are organized into different categories, corresponding to the type and language of a project. By default the predefined ones include templates for Web Applications (ASP.NET) and generic applications (Console, WinForm, Libraries, etc.). There are also User defined ones as well as Shared Solution and Personal Solution, again similar to Live Templates.

On the right-hand column we have a repeated set of templates. What these allow us to do is customize the usage of templates. However, before seeing that and delving into creating new templates, let’s see how we can use the existing ones.

Using Templates in Web Applications

1. Create a new Web Application (can be MVC or WebForms) to work with.

2. There are multiple ways to create new file templates with ReSharper (right-click Solution Explorer |Add | New From Template or via the ReSharper | Edit | New From Template menu). However, by far one of the easiest and most efficient ways is what we introduced in ReSharper 5: Create New File. You can invoke this from anywhere, be it the Solution Explorer or from a file in the editor, by pressing Ctrl+Alt+Ins

image 
[Pressing Ctrl+Alt+Ins in ASPX editor]

 

image 
[Pressing Ctrl+Alt+Ins in Solution Explorer]

Since this is a Web Application, the list of Templates to choose from include all those that appeared under ASP.NET Projects in the Template Explorer. In addition, since creating a Class, Interface, Struct or Enum are still valid in the context, it will display those too.

3. Select a new file, for instance Web form with master page and provide it with a name and hit OK (Enter key)

SNAGHTML2cee1c61

4. Based on the type of file, and in particular the template it contains, we will be prompted for additional information. In this case since we requested a Web Form with a MasterPage, ReSharper prompts for the MasterPage file

image

5. Select the corresponding master page and hit Enter.

We’re done. With very little effort, we’ve created a new Web Page and referenced the Master Page. Comparing that to the default way of creating Web Forms (or in fact any other item) in Visual Studio, we can quickly see how much more efficient it can be. However, the true power of this lies in it’s customization, not only in the template but also in what templates we can choose from.

Customizing the Files we care about

When invoking the Create New File, the list that appears is customizable, both in regard to the contents as well as the ordering. Imagine for instance that we usually work with MVC applications and Spark View Engine and are not really interested in WebForms. Having things like Web Control or Web Form with master page pop-up only add noise to the options. Fortunately we can eliminate these easily:

1. Open up the Templates Explorer editor via ReSharper | Live Templates and select File Templates

2. Select the element we don’t want to appear when creating a new File Template. In this case we want to eliminate Web Controls and Web form xxxx

SNAGHTML2d038f50

3. Click on the red cross (X)

4. Close the Templates Explorer

5. Invoke the Create New File menu by pressing Ctrl+Alt+Ins. It should now only display the remaining entries.

image

Apart from eliminating entries, we can also change the sort order by dragging the entries in the Templates Explorer (right-hand column, same place we selected to delete in step 2) up and down.

Creating new File Templates

Being able to generate templates fast, customize them and edit them wouldn’t be sufficient if we couldn’t also create new ones. Fortunately, we can. Let’s see how we can easily create simple DTO objects that use for instance the DataContract and DataMember attributes.

1. Create a new project. Any type will do.

2. Open Templates Explorer via ReSharper | Live Templates and click on File Templates

3. Select the User Templates node and right-click to create New Template

image

3. In the Template Editor, type DTO for the Description and NewDTO.cs for the file name

4. Enter the following in the Editor

SNAGHTML2d410809

We now need to define some macros on the placeholders (variables $nameSpace$, $className$, etc.).

Note: The first [DataMember] property we’ve added is of course optional and more for demonstration purposes.

5. Select the nameSpace placeholder in the right-hand column and for the value choose “Default namespace for current file

6. Select the className placeholder in the right-hand column and for the value choose “Current file name without extension

7. Uncheck the Editable Occurrence on previous two entries. This prevents ReSharper prompting us for values

8. Save changes and close the editor

Using the new File Template

We saw that File Templates can be invoked using Ctrl+Alt+Ins (Create New File). However if we do this now, our template will not appear. That is because we need to tell ReSharper we want it on this quick list. We can do this in two ways:

Method 1: Invoke the template using an alternative option, for instance ReSharper | Edit | New From Template and selecting More..

image

This will bring up a dialog box with other templates:

SNAGHTML2d4b70d9

where we can select our Template. At this point we can tick the CheckBox “Add to quicklist”, selecting the Category from the Dropdown below it.

Method 2: Alternatively, we can drag and drop the template from the Template Explorer to the Category we are interested in (for instance C#)

SNAGHTML2d50a8da

Either way we choose, from this point on, our template will now be accessible via Ctrl+Alt+Ins:

image

Summary

With File Templates we can easily create files that are used frequently, such as DTO’s, Unit Tests, Controllers or other common classes we create. In addition to providing an efficient way to create these items, we can also interact with them, just like we do with Live Templates to adapt them to our needs on each creation.

Tags:  
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Highlighting Custom Patterns with ReSharper

Thursday, 19 August 2010 15:33 by hadi

A new feature that has shipped with ReSharper 5 is the Structural Search and Replace. It is a way for us to locate certain patterns in our code, and optionally replace them. However, it is more than just a Find and Replace. It allows us to somehow extend ReSharper’s inspection settings by offering us means to identify patterns and highlight them.

As many of you know, ReSharper offers a series of Suggestions, Hints, Warnings and Errors when it detects certain code patterns. For instance, when explicitly declaring a variable, ReSharper suggests you use var (and with good reason)

image

These are configurable, in that you can tell ReSharper whether you want them to be displayed as hints, suggestions, warnings or errors, which is done via ReSharper | Options | Inspection Severity

image

Setting it to Show as error for instance will display a red (or whatever color you have configured) underlining and the right-hand side border will display the a Red box indicating an error in the file.

SNAGHTML12140a89

If we now combine this with Solution Wide-Analysis, ReSharper will also indicate an error in the solution.

image

 

Extending Inspections

Have you ever encountered the following?

image

This is a perfect candidate for using String.Format. How about this?

image

Did you know StringBuilder has an AppendFormat? You might know that, I might, but maybe other developers don’t. It would be good to provide developers with a hint or suggestion that they could use AppendFormat. Up to version 5 of ReSharper, the inspections were limited to what ReSharper provided out of the box and the only thing we could do to extend this functionality was create/use a plug-in We can now use Structural Search and Replace to accomplish this. Let’s see how it works.

1. Open up a Solution (I’m using BlogEngine.NET but feel free to create your own with some sb.Append calls).

2. Select ReSharper | Tools | Pattern Catalog.

SNAGHTML1228e7ce

By default, the Pattern Catalog is empty.

3. Click on the Add Pattern

SNAGHTML122b7d0a

4. In this first sample, all we want to do is highlight code, so click on the Find button at the top to switch the dialog to Find mode only.

SNAGHTML122d5169

The Search Pattern is what we’ll use to define what we are looking for. The Description is a text which helps us identify the pattern easily in the Catalog. The box on the right is for place holders which we’ll see in a moment.

5. Enter the following pattern in the Search Pattern editor

image

What this does is indicate the pattern we are looking for. The values surrounded with $’s (same notation as that used in Live Templates) represent placeholders. A placeholder can be an expression, statement, type, etc. If it is red, it means it has not yet been defined (next step).

6. Click on the Add Placeholder button on the right box and select Expression, and enter the following values:

SNAGHTML12c3cb9a

We are indicating that the placeholder sb (first one used) is an expression of type System.Text.StringBuilder

7. Click on the Add Placeholder button to define the second placeholder, args. Select Argument and enter the following values:

SNAGHTML12c63dfb

We can optionally indicate whether we want to limit the number of minimum or maximum arguments.

If both placeholders have been defined correctly, the Search pattern box should now look like this:

SNAGHTML12c9a0e2

(we’ve entered a description at the bottom)

8. We need to define the type of severity we want. In this case we want it to be marked as a Suggestion. We do that using the Pattern severity dropdown and then click Save

Our Pattern Catalog should now be updated to reflect this new pattern

SNAGHTML12caa7e4

9. At this stage we can either close the Patterns dialog or Search for a pattern. Let’s see what happens if we click Search now button.

SNAGHTML1376eb2e

A Find Results window is displayed highlighting everywhere where ReSharper can find a pattern match. But what about the Pattern severity? Where does that come into play? Let’s double-click on one of the entries:

image

See the highlighting of the sb.Append call? Set the Pattern Severity to Error and that will display in Red.

image

Turn on Solution-Wide Analysis and that will list as an error!

So as we can see, the Structural Search (we haven’t replaced yet), allows us to monitor for certain code patterns in our projects. Potentially we could use it to detect certain code smells.

Fixing things up

Now that we have certain patterns located, and even highlighted, how do we go about fixing things? As we saw in the previous screenshots, we have a little icon pop up when placing the cursor on the line:

image

This context action however doesn’t offer us anything in order to fix the situation, i.e. there is no QuickFix. The reason is obvious: ReSharper doesn’t know what a fix is. Let’s show it!

1. Open up the Patterns Catalog and Edit the previously created Pattern

2. Click on the Replace button to open up the Replace editor at the bottom

SNAGHTML1384f656

3. Enter the following text in the Replace pattern section

image

4. Provide a description (this is what the hint of the QuickFix will dispaly)

image

5. Click on the Save button

Locate the previous sb.Append call and place the cursor on top of the line. We can now see a QuickFix icon appear (A red bulb in this case since it’s an Error)

image

Alt+Enter and we’re done!

image

 

Global Fix

What we’ve just done is a manual local fix, that is, locate the offending entry and hit Alt+Enter to apply a QuickFix. We can do this at a global scope by using the Pattern Catalog tool window.

1. Undo the prefix fix (so as to have several instances)

2. Open up the Patterns Catalog

3. Select the recently created Pattern and click on Search now. This time, instead of the Find Results dialog, we get a Replace dialog which displays all matching patterns and a Replace button

SNAGHTML138b5c29 

4. We can select the entries we want replacing (by default all checked). Click Replace

We’re done! ReSharper will now replace all occurrences. So we’ve applied a QuickFix globally.

 

There’s more

What we’ve seen here is just a simple example of how to improve on a call to a class. We can of course do more complex searches, allowing us to identify code smells, etc. The pattern we created in this tutorial is actually included in a Patterns catalog you can download from the documentation section of our web site (or click here for direct link). All you need to do is unzip it and import the XML from the Patterns Catalog window (Import button). Currently, Pattern Catalogs are PER solution.

There’s much more planned for Structural Search and Replace in vNext and we are always happy to receive feedback, so please try it out, and let us know what you think.

Tags:   ,
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Losing templates on ReSharper upgrade

Monday, 16 August 2010 19:09 by hadi

Derick Bailey pointed out today on Twitter that during an upgrade, he lost his ReSharper templates.

image

 

ReSharper should import the existing templates during an upgrade. However, for some odd reason, sometimes this does not occur. We managed to sort out Derick’s issue relatively quickly, but in case others might run into the same problem, here’s how to solve it:

Scenario: Upgrading from 5.0 to 5.1 in VS2010 and settings lost.

1. Close down Visual Studio 2010.

2. Go to the %AppData%\JetBrains\ReSharper folder. In there you should find subfolders for each version of ReSharper. Select v5.0.

 image

Inside each folder there are subfolders corresponding to the different versions of Visual Studio you have installed on your machine. Select vs10.0 (corresponds to VS 2010).

image

3. Select the files UserSettings.xml and Workspace.xml.

4. Copy the selected files to %AppData%\JetBrains\ReSharper\v5.1\vs10.0

5. Re-start Visual Studio

 

If all goes well, you should now have your settings correctly imported. If you have run into this problem, please feel free to contact me. We need information on setups to try and see what the common denominator is for when this fails (also please store the contents of these folders since it might help us figure out what’s wrong).

Tags:  
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Templates Galore: Live Templates

Friday, 6 August 2010 21:37 by hadi

A couple of months ago I was doing a talk at DevSum, Stockholm. It was a room full of developers, 90% of whom had heard of ReSharper and 80% of them used it. What surprised me however was that only a couple of folks knew about Live Templates, which left me wondering how many outside of the that group actually knew about them too. Thus, in an attempt to expand the reach, we’ll take a look at what Live Templates and in future posts look at Surround and File Templates

Snippets on Speed

Live Templates enhances the Visual Studio Code Snippets by providing more functionality and easier management. To open up the Live Templates manager, we click on the ReSharper menu and select Live Templates:

SNAGHTMLa24f3d5 

ReSharper has four groups of templates:

  • Predefined: Predefined by ReSharper
  • User Templates: Templates defined by the user
  • Personal Solution Templates: Templates that are specific to a solution
  • Shared Solution Templates: Templates that are specific to a solution and can be shared with others

Each group consists of various categories based on the type of project/files we are working on. Many of these also import the default Snippets that are available in Visual Studio. From this screen there are various operations we can perform such as adding, removing, editing templates, as well as importing and them. Editing and Creating Templates is where some of the juice is, so let’s see how we can create a new template.

Creating Templates

Templates consists of literal strings and variables. The idea is to combine these to maximize efficiency when typing. They can be used for a variety of things, from creating classes, DTO’s or skeletons for unit tests, among others. Let’s say we like to create entities that usually have an Id field prefixed (this is not personal!) with the name of the class, along with some other properties. The type of my Id is not necessarily known beforehand. It can be of type string, GUID, etc. Here’s a sample:

public class Customer
{
    public string CustomerId { get; private set; }
    public string Name { get; private set; }
    public string Email { get; private set; }
}

Let’s see the step-by-step process to create a template for this:

1. Open up the Live Template Editor and select User Templates

2. Click New Template to add a new template

image

3. This opens up the editor where we can type our text (literal strings and variables).

image

4. We need to provide a shortcut for our template, which will be used to invoke it (we’ll see how later). Type “be” in the Shortcut field. In the Description type “Business Entity”

5. Type the following in the text area (black in the figure above)

image

The different colors have meanings (these Colors can be configured under Tools | Options | Fonts and Color. Look for “ReSharper Live xxxxx” entries). There are 3 types of elements in the text:

  • text literal
  • template keyword
  • variables

The text literal is everything that is in white text, such as “public class” and “{ get; private set;}”. Anything literal will appear as is when we invoke the template. The second type of element is the $END$ keyword. This is used to tell ReSharper where we want the cursor to be positioned once we have finished invoking and interacting with the template. Finally we have the variables. These are elements that can be used to interact with the template. A variable is designated using the $ prefix and suffix. That is, anytime we type a word prefixed and suffixed with a $ sign, ReSharper will automatically convert it to a variable. The effect that this has is that when we invoke the template, we can iterate through the different variables to type a value. In our case, since we want to establish the classname during its invocation, we make the $className$ a variable. The same occurs with $idType$, since we don’t know what type we want the Id to be (this is for demonstration purposes, normally most entities have the same type for their Id). Finally comes the Id property. We said we want this to resemble the classname suffixed with Id. As such we just suffix it with the word Id.

6. Close the Editor.

7. Open up an existing file and locate an empty are where we can create a new class. Now type “b” and we should get Intellisense for all templates that start with “b”. Locate “be” and hit TAB (or ENTER)

image

ending up with:

image

ReSharper has generated the core code of the template and is now ready for our input. The cursor is currently positioned at className, where can type in the class name. Notice that as we type, the value of the property name is automatically updated, as expected. Once we finish typing className we hit TAB (or ENTER) and ReSharper positions us at the IdType entry where we can type in the type of the property (with the corresponding IntelliseSense).

8. Once the process is complete, we should end up with the following class (for the value “Customer”):

image

Classifying the Template

Currently the newly created template has been classified under “No Languages”. The main issue with this is that this template will be available pretty much everywhere, even places where it wouldn’t make sense to create a new class.

image

Let’s proceed in limiting it in scope and defining a category:

1. Select ReSharper | Live Templates and Click on Edit on the newly created template

2. Select the link at the top of the editor next to the “Available” text

image

The Template Availability dialog box will appear

SNAGHTMLb3ffe17

3. Select the Language-Specific option and choose C# from the dropdown list. The dialog box should now change to show language specific options:

SNAGHTMLb40cf5a

4. We can now choose the minimum version of the compiler for which this template is available (for example if it contains lambda expressions, we cannot use it versions below 3) and also choose where we want the declaration to be available. In our case we can choose Everywhere.

5. Last thing left to do is define a New Category for this template. We can do this by selecting it in the Live Templates tree, right-clicking and selecting New Category. In our case we would type C#

With this change we restrict where this template will be available and have a better classification when viewing templates.

Extending Functionality with Macros

We saw that when multiple instances of a variable appear in the template definition, as one changes, others will update to reflect that change. However, sometimes we might want a slight variation of a specific variable. Say for instance, we are create a template to define a property with a backing field (this template already exists by the way). In this scenario, usually the backing field name is the same as that of the property name with a lowerCamelCase. Let’s see how we can define a template like this using the Macros in the Live Editor Template:

1. Create a new template following the steps outlined previously, ending up with the following definition:

image 

If we were to use this template as is, ReSharper would prompt us to enter a value for $className$, $IdType$ and $backingField$. Ideally what we would like is to have the latter to be a variation of the $className$, concretely, a lowercase version.

2. On the right side of the template editor, there is a Window that displays Macros that we can apply to variables. There is one for each variable. Click on the backingField entry and select the Choose Macro link

image 

A dialog box appears giving us a series of operations we can perform on the variable, from suggestions, conversions, metadata, etc. In our case we need to scroll right to the end and select “Value of another variable with the first character in lower case”:

SNAGHTMLc8bc8fb

3. Once we click OK, the Macro window will then be updated to reflect the change and allow us to define the value of the variable. Click “another variable” link and select one className.

image

4. Save the template.

5. Invoke the template by typing “be” as before. We should end up with:

image

We can now type the className. As we do, we see that the backing field will automatically update to reflect the changes.

image

This is just a simple case of applying a Macro. We can have multiple Macros in the same template and combine them to achieve powerful templates.

Additional Template Options

At the top of the template editor, there are two additional check boxes:

image

The first one is used to indicate whether we want the template to be reformatted according to the defined styles we have set up in ReSharper once completed. The second option is useful when we are referencing namespaces that are not in the using directives. Let’s imagine for instance that we generate a template to spit out some Unit Test fixtures for NUnit. If we were to decorate the class with a [TestCase] attribute in a blank file, ReSharper would indicate an error saying the the namespace is not imported. The way to solve this issue is to fully qualify the attribute when defining the template and select this checkbox. ReSharper will then make sure it imports the correct namespaces and shorten the attribute.

image

 

Sharing Templates

Any template created under the User Templates is visible to the current Windows user for any solution. If we want to have templates specific to a particular solution, we would need to create them under the Personal Solution Templates. These however again are restricted to the current user. If we want them shared across multiple users we need to place them under Shared Solution Templates.

When it comes to sharing templates across machines with team members, the cumbersome way would be to export templates and have each team member import it. The preferable way is to place these templates under network drive and then mount them using the Mount File Storage

image 

By doing this, ReSharper is aware that these templates are shared across machines and makes sure they are updated accordingly.

Summary

Live Templates in ReSharper offer us a lot of opportunities to be more effective when writing code. It is not about code generation but about not wasting time having to type repetitive things. It is also important to understand that they should not replace good programming practices. For example, using templates to write out repetitive code for logging is not the best way to approach the problem. A better solution would be to use Aspect Orientated Programming. However, there are many case where templates are very efficient. In future blog posts we’ll examine the Surround and File templates that ReSharper also offers us.

Tags:  
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (4) | Comment RSSRSS comment feed

Advanced scenarios with dotCover Console Runner

Thursday, 5 August 2010 10:20 by Hadi

In an earlier post, we saw how to use dotCover in simple scenarios, whereby we have a single unit test project for which we want to obtain coverage reports on. There are times however, when this is not the case; that is, we might have multiple tests projects. It is very common (and recommended practice) to separate Unit test projects from Integration test projects, allowing us to run the former more frequently during development and the latter during an Automated Continuous Integration setup. In these cases, running the dotCover analyse command, as shown in the previous post might not be ideal.

 

Fine tuning the coverage steps

For the scenarios described, whereas we have multiple test projects, dotCover allows us to fine-tune the coverage process by offering us a series of commands that breakdown the coverage, merging and reporting into individual steps. Suppose we have the following Project Layout

 

image

The AppCode is our application code that we want to run coverage for, and TestProject1 and TestProject2 are specific the test projects that cover AppCode. These can be Unit Test or Integration Test projects. The process we need to follow is shown in the figure below:

 

image 

 

 

We run coverage on each of the tests projects by using the cover (c) command using the following configuration file (corresponds to testProject1.xml. testProject2.xml is similar)

<?xml version="1.0" encoding="utf-8"?>
<CoverageParams>
  <Executable>
    X:\Libraries\nUnit\nunit-console.exe
  </Executable>
  <Arguments>TestProject1.dll</Arguments>
    <WorkingDir>TestProject1\bin\debug</WorkingDir>
    <Output>output1.xml</Output>
    <Filters>
      <IncludeFilters>
        <FilterEntry>
          <ModuleMask>AppCode</ModuleMask>
          <ClassMask>*</ClassMask>
          <FunctionMask>*</FunctionMask>
        </FilterEntry>
      </IncludeFilters>
      <ExcludeFilters>
      </ExcludeFilters>
    </Filters>
</CoverageParams>

[Command to execute: dotcover c testProject1.xml]

Executing this command generates a snapshot file with coverage data, the Coverage Results Descriptor (by default, unless specified in the configuration file using the TempDir element, the snapshots are saved in the User temporary folder). These snapshots are not the final XML report that we are after but intermediate information that dotCover uses to generate reports from. There is a correspondence between snapshots and processes, such that each process generates a single snapshot. In our case, since we are calling a unit testing framework to launch a single test project, there will only be one snapshot. However there are cases, for instance when running coverage report on an application, whereby the application can launch another process  itself. In this case, multiple snapshots will be generated.

When we run this command, once for each Project, we end up with two snapshot files. We now need to merge the snapshots in order to generate the XML report. This is accomplished using the merge (m) command

<?xml version="1.0" encoding="utf-8"?>
<MergeParams>
  <Source>
    <string>output1.xml</string>
    <string>output2.xml</string>
  </Source>
  <Output>output_merged.xml</Output>
</MergeParams>

[Command to execute: dotcover m merge.xml]

which takes the output of the two cover commands (output1.xml and output2.xml) and generates a single snapshot file. The only thing remaining is to generate an XML coverage report based off of this data, using the report command

<?xml version="1.0" encoding="utf-8"?>
<ReportParams>
  <Source>output_merged.xml</Source>
  <Output>results_merged.xml</Output>
</ReportParams>

[Command to execute: dotcover r report.xml]

providing us with the XML report containing coverage information (percentage of code covered, statements covered), which like in the previous post we can apply an XSLT to format into HTML or JSON.

Other Coverage Commands

dotCover provides two more commands:

  • list
  • delete

The list command simply obtains a list of all snapshot files given the coverage descriptors, that is, the output of the different cover commands. The configuration file lists these descriptors:

<?xml version="1.0" encoding="utf-8"?>
<ListParams>
  <Source><!-- Required. At least one child element expected. -->
    <string><!-- Coverage results descriptor 1 file name --></string>
    <string><!-- Coverage results descriptor 2 file name --></string>
    <string><!-- Coverage results descriptor N file name --></string>
  </Source>
  <Output><!-- Required. Resulting file name. Stores plain list of all snapshot files. --></Output>
</ListParams>

 

The delete command deletes the list of given snapshots:

<?xml version="1.0" encoding="utf-8"?>
<DeleteParams>
  <Source><!-- Required. At least one child element expected. -->
    <string><!-- Coverage results descriptor 1 file name --></string>
    <string><!-- Coverage results descriptor 2 file name --></string>
    <string><!-- Coverage results descriptor N file name --></string>
  </Source>
</DeleteParams>

 

Summary

As we can see, dotCover allows us to fine-tune the coverage process by letting us control the different steps. One question that might arise is why couldn’t we have done the same by running the analyse command twice, once for each Test Project. If we were to do that, we would get two independent reports, whereas here the results are all merged into a single report. The analyse command is a great fit for simple scenarios that only have a single project. Once we move onto more complex setups, having a finer control over the coverage process has its benefits.

Tags:   , ,
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Sony VPCZ1 Review

Friday, 30 July 2010 23:57 by Hadi

sony vaio z seriesRecently I purchased a Sony VAIO laptop and after having it for nearly two months, I have to say, it’s by far the best laptop I’ve owned to date (previous ones included the older VAIO Z series, HP, Toshiba, Fujitsu, LG). I purchased mine online (www.sony.es) and you can customize it to your exact needs. Prices start at 1.500 Euros up to 5.000 Euros (512GB SSD + Blu-Ray recorder, etc.). I went for the modest option of the lower end processor and regular SATA HDD (7400 rpm).

 

Screen

The screen is extremely bright and of amazing quality. It’s 13.1 inches with a resolution of 1600 x 900. There’s an option for 1920 x 900 although it adds around 150 Euros to the price. I’ve got the former and it’s more than enough space. It’s also small enough to work comfortably on the plane.  It also comes with a Hybrid Graphic System, controlled by an external button, allowing you to switch from external to internal graphics to save on battery life.

Speed

Overall I get very good performance out of if. I purchased the lower end CPU, which is the i5 2.40 Ghz and despite that, I’m very pleased. I do quite a bit of video rendering, recording, etc and it behaves! I did however go for 8GB of RAM (which only cost 80 Euros more, over the default 4GB). I'm running Windows 7 Ultimate 64bit.

Touchpad

Nothing out of the ordinary. The mouse keys a little on the tough side. The fingerprint reader is located between both buttons and on the whole works well, although I’m glad that you can still type in a password in those rare cases it doesn’t like your fingers.

 

Keyboard

The touch and layout of the keyboard is amazing. The spacing between keys is more than sufficient for comfortable typing despite the overall small size of the laptop. The cursor keys are conveniently located and the corresponding PgUp, PgDwn are function keys on the cursors, making navigation very smooth. Insert and Delete keys are also positioned in an adequate location (looking at you Toshiba!). I also went with the optional backlight which allows me to work nicely in darker environments (uses sensor to detect light). As with many laptops, it comes with a couple of VAIO specific buttons (Support, Care, etc. which I hardly use).

Battery life

I went with the extended battery which supposedly gives me around 10 hours of battery life in Stamina mode (there are three modes: Automatic, Speed and Stamina, controlled by an external button). Have I ever managed to get 10 hours of battery life out of it? Keep on dreaming. I’ve got about 5-6 hours on Auto mode which to be honest is not bad at all. Initially I was concerned about the extra battery since the web site does indicate that it sticks out. Now if you’ve seen the Lenovo’s you know that usually means a bad case of hemorrhoids. However, Sony have been clever here in that they’ve designed the battery to act as a leaver. Instead of sticking out from the back, it sticks out from underneath. This provides two benefits: it raises the laptop a bit, which usually people like their keyboards raised, and by lifting the laptop from the table, it provides more room for air circulation, keeping it running cooler in general.

Weight and Size

Extremely light. Specs indicate 1.4Kg. The extra battery adds a bit more weight to it, but overall, very light. And in all in all quite compact. As I mentioned previously, can work perfectly in a plane with it.

Heat

One of my main concerns with laptops is heat. Unfortunately the heat normally is dissipated where you rest the palms of your hands (left and right of the touchpad) or on the keyboard. In this case, the area around the touchpad is completely cool. You can detect a bit of heat on the left side of they keyboard (near the QWERTY keys) when working with it extensively. However, nothing out of the ordinary (looking at you Fujitsu and HP).

External inputs and other features

Other features it includes which come in very handy:

  • SD Card Reader
  • Sony MS and HG Duo
  • Built-in Motion Eye Camera
  • Built-in Microphone/Speaker and external jacks in front of the keyboard
  • External button for switching off WiFi/Bluetooth
  • VGA and HMDI output
  • 3 USB ports (you can pick 2 + 1 iLink)
  • Network port
  • Optionally WAN (I didn’t go for that option)

Case and Design

The outer case comes in various colors and material. I went for the black Carbon and it looks quite nice. They have platinum, etc. As for the overall design, sorry Apple fan boys, new kid in town.

Support

 

I purchased the laptop with the default OS because like many, I prefer to clean it down and re-install. Being a newer model, I was “pleasantly” surprised to find that none of the drivers for my exact model where available on the site. I called Sony support expecting to be brushed off and instead I received extremely good support. They were very attentive to my issue and every email I sent was replied to in under 24 hours. Fortunately I found drivers from similar models and got it all configured and apparently my drivers should now be available on the web (although I’ve not checked yet). All in all though, very happy with the service I’ve received so far. Extremely helpful and pleasant to deal with.

Summary

To be honest there are very few things I don’t like about the laptop. I didn’t appreciate much the fact that they’d sold me something without the drivers being ready for download, but I worked around that as I mentioned. About the only thing I don’t like are the speakers. The volume is not very high. Even at full-blast it’s something like 40% of my previous LG laptop. The headphones have the same levels. Plugging in external amp solves it though. Other than the speaker level though, there’s not much more I could say negatively about the laptop.

As I said earlier, out of all the laptops I’ve owned (I’ve been working exclusively on laptops for the past 10 years more or less), this is by far my best purchase. It’s extremly lightweight, powerful and feature rich. Considering the price I paid for it, it’s well worth the investment. I highly recommend it.

Tags:   ,
Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed