Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What resharper 5 patterns do you use? [closed]

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.

like image 544
Squirrel Avatar asked Nov 01 '10 21:11

Squirrel


2 Answers

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
like image 101
rsenna Avatar answered Sep 23 '22 18:09

rsenna


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.

like image 40
Jura Gorohovsky Avatar answered Sep 22 '22 18:09

Jura Gorohovsky