Reshaper Live Templates for validating arguments

I strongly agree with the ‘contract‘ mataphor in API design, hence my interest in Spec# (that I’ve blogged about before).

To assist in this and to eliminate the tedium of writing argument checks, I’ve created 3 Resharper Live Templates. These check for

These templates are called arg, argnull and argempty respectively.

Resharper 1.5 doesn’t allow importing or exporting of Live Templates from the GUI. They are however stored in an XML file at %userprofile%Application DataJetBrainsReSharperUserSettings.xml. Below is a chunk of XML that must be placed under the TemplateManagerTemplates node. If you have any others, please let me know!

<Template text=”if( TYPETYPEEXPRESSIONEXPRESSION )&#xA; throw new ArgumentException( @&quot;Cannot ACTIONACTION as the value passed was invalid. Please provide a valid value.&quot;, @&quot;TYPETYPE&quot; ) ;&#xA;” shortcut=”arg” description=”Throws an ArgumentException” kind=”livetemplate” reformat=”false” fileMask=”” context=”Everywhere” fileContext=”CSharp” enabled=”False” id=”1723695683″>

<Variables>

<Variable name=”TYPE” expression=”variableOfType(&quot;System.Object&quot;)” initialRange=”0″ />

<Variable name=”EXPRESSION” expression=”constant(&quot; != 1&quot;)” initialRange=”0″ />

<Variable name=”ACTION” expression=”constant(&quot;PERFORM AN ACTION&quot;)” initialRange=”0″ />

</Variables>

</Template>

<Template text=”if( TYPETYPE == null || TYPETYPE.Length == 0 )&#xA;throw new ArgumentNullException( @&quot;TYPETYPE&quot;, @&quot;Cannot ACTIONACTION as the value passed was null or empty. Please provide a valid non null value.&quot; ) ;&#xA;” shortcut=”argempty” description=”Throws an ArgumentNullException when a string is null or empty” kind=”livetemplate” reformat=”true” fileMask=”” context=”Everywhere” fileContext=”CSharp” enabled=”False” id=”1705884294″>

<Variables>

<Variable name=”TYPE” expression=”variableOfType(&quot;System.String&quot;)” initialRange=”0″ />

<Variable name=”ACTION” expression=”constant(&quot;PERFORM AN ACTION&quot;)” initialRange=”0″ />

</Variables>

</Template>

<Template text=”if( TYPETYPE == null )&#xA;throw new ArgumentNullException( @&quot;TYPETYPE&quot;, @&quot;Cannot ACTIONACTION as the value passed was null. Please provide a valid non-null value.&quot; ) ;&#xA;” shortcut=”argnull” description=”Throws an ArgumentNullException” kind=”livetemplate” reformat=”true” fileMask=”” context=”Everywhere” fileContext=”CSharp” enabled=”False” id=”1497171628″>

<Variables>

<Variable name=”TYPE” expression=”variableOfType(&quot;System.Object&quot;)” initialRange=”0″ />

<Variable name=”ACTION” expression=”constant(&quot;PERFORM AN ACTION&quot;)” initialRange=”0″ />

</Variables>

</Template>

🙏🙏🙏

Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.

Leave a comment

Comments are moderated, so there may be a short delays before you see it.

Published