views:

99

answers:

4

Resharper 5's new pattern matching seems very powerful, though it takes a bit of tinkering to work out how to use it.

For those who aren't familiar with this feature, it allows you to search for specific patterns within your code. Instances of such patterns may optionally be replaced with an alternative. In IntelliJ this was called structural search and replace. It's much more powerful than simple RegEx search/replace.

I'd like to collect a series of patterns that people are using so that I can learn how to use this feature better.

I propose that each answer include:

  • a brief introduction of the rationale for the pattern
  • an example of what it would match
  • an optional example of a replacement
  • the XML generated by exporting the pattern so that others can try it out too
A: 

Restarting a Stopwatch

.NET 4 introduces the System.Diagnostics.Stopwatch.Restart() method which can tidy up your code.

Before:

stopwatch.Reset();
stopwatch.Start();

After:

stopwatch.Restart();

XML:

<CustomPatterns>
  <Pattern Severity="SUGGESTION">
    <Comment>Use Restart method for System.Diagnostics.Stopwatch</Comment>
    <ReplaceComment>Use Restart method for System.Diagnostics.Stopwatch</ReplaceComment>
    <ReplacePattern>$stopwatch$.Restart();</ReplacePattern>
    <SearchPattern><![CDATA[$stopwatch$.Reset();
$stopwatch$.Start();]]></SearchPattern>
    <Params />
    <Placeholders>
      <ExpressionPlaceholder Name="stopwatch" ExpressionType="System.Diagnostics.Stopwatch" ExactType="True" />
    </Placeholders>
  </Pattern>
</CustomPatterns>
Drew Noakes
+2  A: 

Matching a [Flags] enum bit

.NET 4 introduces the System.Enum.HasFlag method which can tidy up your code.

Before:

(myValue & MyFlagsEnum.Foo) == MyFlagsEnum.Foo

After:

myValue.HasFlag(MyFlagsEnum.Foo)

XML:

<CustomPatterns>
  <Pattern Severity="SUGGESTION">
    <Comment>Can condense using 'Enum.HasFlag' method</Comment>
    <ReplaceComment>Replace bit matching with 'Enum.HasFlag'</ReplaceComment>
    <ReplacePattern>$myValue$.HasFlag($target$)</ReplacePattern>
    <SearchPattern><![CDATA[($myValue$ & $target$) == $target$]]></SearchPattern>
    <Params />
    <Placeholders>
      <ExpressionPlaceholder Name="myValue" ExpressionType="System.Enum" ExactType="False" />
      <ExpressionPlaceholder Name="target" ExpressionType="System.Enum" ExactType="False" />
    </Placeholders>
  </Pattern>
</CustomPatterns>
Drew Noakes
Nice. I always wrote a private method to do this as I hated repeating myself, but now I know better! :)
Mark Simpson
A: 

JetBrains offer a Sample Pattern Catalog for Structural Search and Replace for download containing 17 patterns:

  • 'try/finally' block can be converted to 'using' statement
  • Method StringBuilder.Append can be converted to StringBuilder.AppendFormat
  • Comparison with true is redundant
  • Conditional statement is redundant
  • Code is unreachable
  • 'if' block is never executed
  • Identical branches in a conditional statement
  • Redundant compound assignment with |= operator
  • Redundant compound assignment with &= operator
  • Redundant compound assignment with |= operator (alternative case)
  • Redundant compound assignment with &= operator (alternative case)
  • Redundant initialization to false and condition block
  • Redundant initialization to true and condition block
  • Method Array.CreateInstance can be replaced with an array creation expression
  • Method Array.CreateInstance can be replaced with a two-dimensional array creation expression
  • Redundant usage of GetType() == typeof() with a value type
  • Method OfType can be used for type-based filtering
Drew Noakes
A: 

For example, Microsoft recommends (and Code Analysis/FxCop generates appropriate warnings) if you are doing a comparison between a string value and an empty string, to use the String.IsNullOrEmpty() method.

http://david.gardiner.net.au/2010/02/resharper-5-structural-search-and.html

nihi_l_ist
R# does this out of the box, as of build 5.1.1757.11 at least. It'd be nice to have support for conversion to `string.IsNullOrWhitespace` though.
Drew Noakes