Mark Needham

Thoughts on Software Development

Archive for the ‘resharper’ tag

Visual Studio/Resharper: Changing the order of arguments

with 6 comments

We've recently run into some places in our tests where the expectation and actual values passed into NUnit's 'Assert.AreEqual' are the wrong way round, therefore meaning that the error messages we get when tests fail are somewhat confusing!

Assert.AreEqual(theActualValue, "the expectation");

We can change the arguments around using Resharper by using the key combination 'Ctrl-Alt-Shift-ArrowKey' but you can only do this one line at a time which was a bit annoying as there were about 20 to change.

I got a bit bored of doing this after a while so I thought I'd look into whether it would be possible to do this with a 'Find & Replace'.

After a bit of trial and error this is what I've ended up with:

  • Select all the areas of code that you want to change and press 'Ctrl-H'
  • In the find box type:

    \({.*}, {\".*\"}
  • And in the replace box type:

    (\2, \1

The '{}' define a matching group of which we define two in this case and then switch them around. Visual Studio's regex seems a bit different than the one I'm used to – the reference list for the syntax is available on MSDN.

It's not too complicated and I'm sure there are edge cases where it wouldn't work but for the little case I had it did the job reasonably well.

Written by Mark Needham

June 23rd, 2009 at 7:31 pm

Resharper templates

without comments

One of the first things that I do when I go onto a project is setup a ReSharper template for writing tests.

I generally set this up so that when I type 'should' I can press tab and it will automatically create an outline of a test method for me.

Creating a template is as simple as going to 'ReSharper > Live Templates' from Visual Studio.

I have attached several templates that I seem to end up writing over and over again.

To import these go to 'ReSharper > Live Templates' and click on the Import button on the menu.

To make class creation templates available on the 'Add New Item From Template' menu right click on the project, 'Add > New From Template > More' then select the appropriate template and tick the box which says 'Add to quicklist'

Written by Mark Needham

August 27th, 2008 at 11:58 am

Posted in .NET

Tagged with , , ,

First thoughts on using var in C# 3.0 with Resharper

with one comment

One of the first things I noticed when coming into the world of C# 3.0 was the use of the key word 'var' all over our code base.

I had read about it previously and was under the impression that its main use would be when writing code around LINQ or when creating anonymous types.

On getting Resharper to tidy up my code I noticed that just about every variable type declaration had been removed and replaced with var. Very confused I asked one of my colleague's what was going on. It turns out that this is intended and is now Resharper's default. I quickly found the option and turned it off.

Subsequent discussions have led me to believe that perhaps it is not such a bad thing. When I first saw the use of var I felt that the readability of the code was severely impacted, and I still think that this is the case if method bodies have a lot of code in.

Therefore using var in your code seems to encourage you to keep the methods short so that people can actually understand what's going on. This leads to much easier to understand and maintain code.

Of course the discipline to make this happen is still needed but in the way that the lack of static typing in Ruby encouraged the writing of unit tests (I am led to believe), hopefully the use of var will encourage short compact methods.

The Resharper blog has a post explaining why they have decided to make var so prevalent in the code refactoring that Resharper does. Included amongst these is better naming of local variables.

Having reading Jeff Bay's essay in The ThoughtWorks Anthology about clean OO design I have become quite fanatical about the naming of variables. I like them to be as descriptive as possible so that I can immediately tell what an object is doing without having to look at the details. Shortcuts such as naming an instance of an Account object as 'acct' are pointless in my opinion and just serve to confuse people.

Any language feature that helps encourage the writing of clear variable names is therefore a very good thing.

Dare Obasanjo seems to dislike this new feature while Joshua Flanaghan is more of a fan so clearly the jury is still out.

Written by Mark Needham

August 15th, 2008 at 8:03 am

Posted in .NET

Tagged with , , ,

Keyboard shortcut for running tests with Resharper

with one comment

Having moved back into the world of C#/.NET development after a few months in the Java world I have had the joy of getting to use Resharper again.

One annoyance that myself and my team have been having over the past few weeks is running unit tests. We always end up going to the Solution Explorer, right click the project and then click 'Run Unit Tests'. There is another way…

From Visual Studio:

  • Click on Tools > Options > Keyboard
  • Select the 'Show commands containing' box and type in 'resharper.resharper_unittest'.
  • Select 'Resharper.Resharper_UnitTest_ContextRun'
  • Assign this to a keyboard shortcut (I usually use Shift-F11 as that's the same as IntelliJ!) and running unit tests need never be a pain again.

resharper-screenshot.jpg

Written by Mark Needham

August 8th, 2008 at 7:23 pm

Posted in .NET, Coding

Tagged with

Do IDEs encourage bad code?

with one comment

Although modern day IDEs (Eclipse, IntelliJ, Resharper etc) undoubtedly provide a lot of benefits when writing code, I am starting to wonder if the ease at which they make things possible actually encourages bad habits.

Useful features such as creating and initialising member variables from the definition of a constructor are quickly nullified by the ease at which one is able to create getters/setters/properties for these same member variables. All hopes of encapsulation gone with a few clicks of the mouse.

The counter argument is that you need to work responsibly when given a powerful tool, but it just seems to me that it's hard enough to write good OO code (too hard maybe?) – anything which makes it harder is not a good thing!

I am convinced that IDEs need to provide an Office paper clip style Martin Fowler which pops up whenever you do something questionable (such as creating getters for every field on a class) and asks whether you really want to do what you're doing.

Or maybe there is too much cranking out of the code and not enough thinking about the design of what we're coding that's the real problem…

Written by Mark

July 27th, 2008 at 11:43 am