What do you use 5 models?

Since resharper 5 now has DIY patterns, what patterns did you write that fix the coding idioms you saw? Is there an online reshar template repository? I thought there would be a logical place to vote for your favorite designs.

I think of it as programming in small .

+7
idioms c #
source share
4 answers

I am currently doing deep refactoring in an outdated application. Here are some ReSharper (6.1) patterns that I use to fix some code issues:

Apply the 'using' pattern

Search Scheme:

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

Replace Template:

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

Apply the 'using' pattern (return to inside)

Search Scheme:

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

Replace Template:

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

Resharper does not recognize the next possibility to use the operator ?? , so I created a template for it. Of course, this conditional assignment becomes a simple assignment (with the same value when $nullable$ not null); However, I find that the resulting code will be easier on the eye.

Use '??' operator

Search Scheme:

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

Replace Template:

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

And finally, one of my favorites:

C # is not Java - you can use "==" to compare strings

Search Scheme:

 $str1$.Equals($str2$) 

Replace Template:

 $str1$ == $str2 
+5
source share

There is no full-fledged SSR directory directory on the Internet, although we want it to be one. It is definitely on the to-do list for the future. However, the ReSharper documentation page has a link to a sample catalog template based on the templates used in the ReSharper command.

+4
source share

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

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

Search Scheme:

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

Replace Template:

 $valType$ $varName$; if ($dict$.TryGetValue($key$, out $varName$)) { $moreCode$ } 
+3
source share

It just turned out that it even existed this morning. The first thing I did was try to make sure that zero or constants are always on the left side of the equality check to prevent the object from being unintentionally set to null.

I haven't figured out the constants yet, but the null check 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 this template and made a simple search template:

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

This will really find any instances in which you set something null in the if statement, regardless of whether you want to do this.

I'm seriously going to lose a few days writing samples. However, my software will be better for this.

EDIT : Here is another one that annoys me in my code base. Since Directory.CreateDirectory checks for an internal directory, there is no point in making an excessive call to Directory.Exists in advance.

 Search pattern: if (!Directory.Exists($path$)) { Directory.CreateDirectory($path$); } Replace Pattern: Directory.CreateDirectory($path$); Placeholders: path: identifier 
+2
source share

All Articles