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

Friday, July 1, 2011

DBNull and null

I am deeply frustrated at this point in time. The reason is because I have been spending the last 30 minutes hung over a very careless mistake. C# has the coalesce operator that allows you to write code that looks neater when you are doing complex stuff like LINQ. The problem looked something like this - I had to read a couple of rows from the database and convert those data as properties on an object.
the code would look something like this:

object x;
int value;
if (x == null)
  value = -1; // default value
else
value = (int)x;

The coalesce operator allows you to make the code more concise by saying

int value = x ?? -1;// less code = less maintainence

but whilst reading data from a database table, if a particular cell is a null value, it is not read as a null but it is read as a System.DBNull. So, the freaking row[field] read was getting read as a DBNull. Once I noticed that this was the problem,  I coul;d fix the code, but the fix was less elegant.
More edits on this post when I find more about it.

[Edit 1]
Found this related question on Stackoverflow [here]


Monday, May 23, 2011

Saturday, May 7, 2011

hello blog

Finally, I figured out a way to post code onto my blog. Thank you @CraftyFella

// Comment
public class Testing {
public Testing() {
print("WooHoo");
}
 


Inequalities

A couple of hacks to get done with Inequalities problems on the GMAT quickly. These might not help you to solve the entire problem. However, these can help you to reduce the amount of time spent in evaluating answer choice, specifically for DS questions.

|x-5| < 2
How do we interpret the above (in)equation? The normal/longer root would be to solve for two equations - one treating the outcome of (x-5) as positive and another treating the outcome of (x-5) as negative. Thus the two equations you would solve would be for (x-5) = 2 and (x-5)= -2 and then finally, replace the "=" with a "<".

A shorter, easier, less error prone method would be to rephrase the (in)equation - read it as x is less than 2 units away from 5. Reading it this way, the answer instantly pops out that x can take values from 3 to 7

Original inequation Rephrasing Range of x
|x+5| < 2 x is less than 2 units away from -5 from -7 to -3
|x+5| < 2 x is greater than 2 units away from -5 from -inf to -7 and -3 to +inf

Saturday, April 30, 2011

Numbers on steroids #2

Yet another GMAT Hack:
What is the mean of the numbers on an arithmetic series a, a+d, ...., a+(n*d)
If we take the last numbers as l,
then the mean  = (a + l)/2.
Nifty Huh?!

Numbers on steroids

AS some of you are aware, I am trying my hand at the GMAT. Nothing serious. I was just bored over a couple of weekends. I Didn't have any code to write so I decided that I was going to write the GMAT. These are three properties regarding perfect squares which I learnt about and I am putting them here for any person who is writing the GMAT. All right I am putting it down here so that I can google myself up later.

These are the props :
  1. A perfect square has an even number of powers of prime factors
  2. Any perfect square integer always has an odd number of distinct factors.
  3. For any perfect square, the sum of its distinct factors is always odd
Now for the substantiations: 
  • A perfect square has an even number of powers of prime factors
This is "THE BASIC RULE" and the other two (and perhaps many others) rules can be derived out of this rule. The rule says - For a perfect square, N, if N is prime factorized, say N = (px) * (qy), x and y can and will only be even integers. This seems pretty obvious. If x and/or y were infact odd, there wouldn't be able to find  sqrt(N) in an even positive integer.

With  that now settled, in order to prove the second point,

  • Any perfect square integer always has an odd number of distinct factors.
there is this tiny hack that lets you find the number of distinct factors a number has. it is a simple two step process. 
  1. Factor the number into its prime components, N = (px) * (qy)
  2. The number of distinct factors =(x+1)*(y+1).
Since we proved just now that for such a perfect square x and y will be even,
 the number of factors = (even + 1)*(even + 1)
                                    = odd* odd
                                    = odd

 The third rule is a bit tricky

  • For any perfect square, the sum of its distinct factors is always odd
I havent arrived at a thorough proof for this one as of now. But one thing to remember is that any perfect square will have an odd number of odd factors and an even number of even factors. So adding all these up, we get an odd integer. Try it. 

Sunday, February 13, 2011