Code Templates In Xamarin Studio

Writing the same code structure over and over seems a bit non productive. Luckily there is a simple way in Xamarin Studio to remove this repetative work and speed up things, __Code Templates__

Basic Code Templates

Out of the box Xamarin Studio offers some default Code Templates. If you type prop followed by tapping TAB twice in the editor, you get a default structure of a property. This will bring the template a live and you can the enter the type press TAB and the the name of the property. All very simple and useful.

Writing your own custom Code Templates

You can use the built in Code Templates, but often you use a third party library with a specific code style or you have some kind of code convention in your team. This is where you quite easy can create your own code templates.

One of the plugins I use very often is MVVMHelpers
The syntax for setting a property with this library is as follow where the property is implemented using a backing field.

As this is not the default syntax for the default prop template (part of Xamarin Studio by default) , we need to implement a new one.
Start by opening Preferences -> Text Editor -> Code Templates. Here you’ll see a list of all the existing templates. Click Add to create a new template.
//IMAGE
Enter a name, description and a group.

Now when it comes to the template we need to follow a specific syntax, as we want the consumer of the template to be able to specify the type and name of our property. As you can see I enter %type% for the type _%name% for the backing field, the _ is just part of my coding style. Finally %NAME% as the name of the property

As you can see this can speed things up a bit and save you from writing boiler plate code over and over. As an enhancement you can also make use of the %end% keyword, to decide where the cursor should be positioned after the template is complete. An idea for another code tempate might be unit testing with the ArrangeActAssert pattern AAA.

How could Code Templates be improved

One thing that would be really great would be the ability to share code templates among your team as you can with Visual Studio and Resharper, or just to have a Code Template sharing repository to consume.