Christian Harlass's Blog

neat .net

ReSharper: Using [NotNull] to generate better code

Posted by charlass on July 30, 2009

My all-time-favorite VS plugin, ReSharper, has amongst all the features one which gets hardly noticed: The Annotation Framework and it’s influence on code generation.

The Annotation Framework is a set of “hints” which can be applied to your code, either directly in form of Attributes or stored as an an external XML file (the ReSharper team was using the later method to enhance the .Net Framework libraries). The attributes, which I usually use, are NotNull and CanBeNull. There are some more but those two give you the greatest benefit.

To use them, add at first a reference to the JetBrains.Annotations.dll to your project. It can be found in the ReSharper installation folder (better add a copy to your project and SCM though).Then you can already start annotating your code, such as:

public class Person
{
  [NotNull]
  public string Name { get; set; }
}

Pretty self-explaing, isn’t it? When trying to assign then null to the person’s name, ReSharper will give you a nice little “Possible null assignment to …” warning. Please keep in mind that this is just a warning – nothing prevents you from compiling and running that code.

However, the coolest thing about it is the impact on code generation. For example, I usually let ReSharper generate my constructor code (Alt-Ins, Generate a constructor which will also initialize my properties). In case of our Name attribute, ReSharper detects the NotNull annotation and automatically adds the required null-parameter check!

public class Person
{
  public Person ([NotNull] string name)
  {
    if (name == null) throw new ArgumentNullException ("name");
    Name = name;
  }

  [NotNull]
  public string Name { get; set; }
}

Now that’s what I call convenience.

Advertisements

3 Responses to “ReSharper: Using [NotNull] to generate better code”

  1. bkoelman said

    Hi Christian, good to see that other people are also using these annotations. I like how they make Resharper smarter about nulls. But forget to add the attributes all the time. So to help me, I created a Visual Studio extension to remind me. Feel free to use it if you like.
    https://github.com/bkoelman/ResharperCodeContractNullability/

    • charlass said

      Sorry for the late reply… Looks promising at a first glance, will try it soon (could benefit in my current Project from it!).

      Did you also notice the Code Analysis rules I wrote earlier on?
      https://charlass.wordpress.com/2013/06/09/enforce-resharper-code-annotations-via-code-analysis-fxcop/

      Maybe we could offer both together, with aligned checking rules and configuration.

      • Bart Koelman said

        Hi Christian,

        I just downloaded your source code. Wow! Now I realize that, two years ago, *your* project was my starting point for writing an enhanced FxCop nullability ruleset!

        But… before I completed my FxCop version, I switched to Roslyn.

        The last days I’ve been in the process of backporting my Roslyn analyzer to my original FxCop version. I’m planning to put it on GitHub, next to my Roslyn analyzer within the next weeks.

        I’ll be glad to mention your name or link somewhere on your blog if you like.

        Regards, Bart

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: