What resharper 5 patterns do you use? [closed]
Asked Answered
S

4

7

As resharper 5 now has DIY patterns, what patterns have you writen that fix coding idioms that you've seen? Is there an online resharper pattern repository? I thought here would be a logical place to vote for your favorite patterns.

I think of this as programming in the small.

Salamanca answered 1/11, 2010 at 21:27 Comment(2)
A Resharper pattern repository would rock!Medicate
Surprised there are not more answers here. Would love to see how others take advantage of this feature.Calices
M
5

I'm currently doing a deep refactoring in a legacy application. Here are some ReSharper (6.1) patterns I'm using to fix some code issues:

Apply the 'using' pattern

Search pattern:

$type$ $var$ = $expr$;
$stmt$
$var$.Dispose();
$var$ = null;

Replace pattern:

using (var $var$ = $expr$)
{
    $stmt$
}

Apply the 'using' pattern (with return inside)

Search pattern:

$type$ $var$ = $expr$;
$stmt$
$var$.Dispose();
$var$ = null;
return $something$;

Replace pattern:

using (var $var$ = $expr$)
{
    $stmt$
    return $something$;
}

Resharper does not recognize the following opportunity of using the ?? operator, so I've created a pattern for it. Of course, it makes a conditional assignment become a simple assignment (to the same value when the $nullable$ is not null); still, I find the resulting code to be easier on the eye.

Use the '??' operator

Search pattern:

if (!$nullable$.HasValue) $nullable$ = $value$;

Replace pattern:

$nullable$ = $nullable$ ?? $value$;

And, finally, one of my favorites:

C# is not Java - you may use "==" for comparing strings

Search pattern:

$str1$.Equals($str2$)

Replace pattern:

$str1$ == $str2
Mauney answered 23/4, 2012 at 16:47 Comment(0)
G
4

No full-fledged online SSR pattern catalog exists though we wish there was one. This is definitely in a to-do list for the future. However, on the ReSharper documentation page, there's a link to a sample Pattern Catalog based on patterns used in ReSharper team.

Giulia answered 1/11, 2010 at 22:10 Comment(0)
K
3

Use IDictionary<TKey,TItem>.TryGetValue instead of looking up by key twice

  • $dict$ is an IDictionary<,>
  • $key$ is exactly one argument
  • $moreCode$ is any number of statements
  • $valType$ is a type
  • $varName$ is an identifier

Search pattern:

if ($dict$.ContainsKey($key$)) {
    $valType$ $varName$ = $dict$[$key$];
    $moreCode$
}

Replace pattern:

$valType$ $varName$;
if ($dict$.TryGetValue($key$, out $varName$)) {
    $moreCode$
}
Keheley answered 21/1, 2014 at 17:33 Comment(0)
C
2

Just found out this even existed this morning. First thing I did was try to make sure null or constants were always on the left side of an equality check, to prevent unintentionally setting an object to null.

I haven't figured out constants yet, but null checking was:

Search pattern: if($expr$ == null) $stmt$
Replace Pattern: if(null == $expr$) $stmt$
Placeholders: 
    expr: expression of type System.Object (or derived type)
    stmt: minimum of one statement, no maximum

Then I copied that pattern and made a simple Find-only pattern:

Search pattern: if($expr$ = null) $stmt$
Placeholders: 
    expr: expression of type System.Object (or derived type)
    stmt: minimum of one statement, no maximum

This one will actually find any instances where you did set something to null in an if statement, whether you meant to or not.

I'm seriously about to lose a few days writing patterns. My software will be better for it, though.

EDIT: Here's another one that irritates me in my code base. Since Directory.CreateDirectory checks for the existence of the directory internally, there's no sense in making a redundant call to Directory.Exists beforehand.

Search pattern:

if (!Directory.Exists($path$))
{
    Directory.CreateDirectory($path$);
}

Replace Pattern:

Directory.CreateDirectory($path$);

Placeholders: 
    path: identifier
Casting answered 5/10, 2011 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.