Visual Studio

Using regex in Visual Studio

This is as much a note to self as a blog post :p

One developer in my team had done null == somevariable everywhere in our solution. I think unified code styles are important. So I wanted to remove them. Standard replace does not work here since we need to move the null after the variable name. So we need to use a regex.


We search for null and then any character as few as possible until we hit a space or a ending parenthesis. We then replace with group 1 ($1) which is the variable name == null and then group 2 ($2) which is either a space or a ending parenthesis.

This ensures above replace works with both if(null == somevar) and code like if(null == somevar && someotherstuff)

Here is a version on steroids that can do both equals and not equals
Regex v2

Visitor pattern navigation support with ReSharper

I love the visitor pattern, it enables open/closed principle which is a great fundamental part of maintainability and clean code. You can read more about the pattern here. There is one down side of this pattern, and that is navigation. Consider this code.

_cqsClient.ExecuteCommand(new MyCommand());

If you navigate to ExecuteCommand you will just end up at some close to the metal code that executes your command handlers. And if you try to find all usages for the Command you will only find usages of its constructor (becasuse of the new keyword).

With vanilla ReSharper you need to first navigate to the class and then do a find all usages on the class declaration and navigate to the command handler from there. Very counter productive. But ReSharper is extendable!