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
Chris doggett
source share