Wednesday, August 24, 2011

HOWTO - Creating a resharper file template





I recently created a Resharper file template that simplifies for me the process of adding a new Test class in a test project and gets me directly to the point where I can start writing the test [without doing any of the boiler plate stuff]. Here is a simple DIY manual for creating a Resharper file template. This is essentially a template for creating a nunit test fixture.
We notice that there are redundant actions that you have to perform every time we want to add a new test class to our project. I therefore want to have a template that does all the plumbing work for me whenever I want to create a new test.

Click on the "Resharper -> File Templates" menu in Visual Studio.
Select the "File Templates" tab in the window that appears and click on the +Add Button
Resharper next displays a create new template window. 

In the Description, add a name for the template(I admit this is oddly named). And in the text area to the bottom left, add the content that you want your template to fill out for you automatically. My template looks like this. 

 Notice the identifier(the rich tokens surrounded by the $s) and the replacing macros to the right of the template body. Resharper allows you to choose the macro that you want simply if you click on the 
Change macro... link. Choose the one that makes most sense to you. 






Tuesday, August 16, 2011

Resharper FileTemplates FTW!!





Sometimes, when you are pairing you feel the need to be more precise in the way you write code. You might want to generate the same amount of code using lesser keystrokes. This has a few un-obvious advantages:
  • that your pair is not going to have to sit bored as you keep pecking on that keyboard
  • with lesser keystrokes, you’re going to write code quicker(duh),
  • with lesser keystrokes, you have fewer errors.
  • You look cooler and more sophisticated.




Even the fastest typists I know are like hulk when it comes to coding - All brawn(Like Hulk). Coding is not just typing. Its more sophisticated(Like Flash).


Normally, creating a test class in c# would involve the following steps.

  1. Navigating to the folder where you want to put your test file.
  2. RightClicking on the folder and clicking add -> Class
  3. Giving a proper name for the test class.

  4. Adding a using NUnit.Framework; line of code at the top of the file
  5. Marking the class as [TestFixture]
  6. Adding [SetUp] and [TearDown] methods
  7. Finally, you get to the test and get to the arrange, act, assert tradition.
One such feature in Resharper is File Templates and its darn useful.

To illustrate, lets try writing a basic Test Class with a basic test and see how many steps it takes.


Alt+Insert on a folder and selecting Test
 




Giving the new Test Fixture a proper name

Voilla


PES - Project Euler 9

A particularly interesting feature of f# is its lazy types. A lazy type is something that doesnt get executed all the time. It gets evaluated only when necessary. At all other times it just gets passed around. In this problem, the objective is to find a particular Pythagorean triple .

Here is a function that calculates a Pythagorean triple given m and n such that m > n.
    let PythagorTriple m n =
            m*m - n*n, 2*m*n, n*n + m*m