During the month of Xamarin, there are a lot of useful snippets. Some will help you with some techniques or tooling, or help you automate the code you write a lot of. I love code snippets and sing their praises as loudly as I can. For your own development they speed things up, and for live presentations, you can often use them to live code faster than you would normally.
Code snippets are extremely useful for a number of other reasons and I thought I’d share my thoughts around them.
The reasons I use snippets:
- When I want to learn something new
- When I do something a lot
- When I want to do something complex but know I’ll do it again.
So let’s breakdown those reasons and take a look at how they fit in the Xamarin world. Let’s take a look.
Snippets for when you are learning something new
When Xamarin.Forms came out I originally made a collection of snippets to make coding Xamarin.Forms easier. I was not at that stage familiar with XAML as I missed the WPF and Silverlight revolution as I was maintaining a WinForms based app that had an ASP.NET backend.
So for me, code snippets were extremely useful, being able to find the appropriate code to create the controls I wanted in a way that I knew would work consistently. I liked XAML but I wasn’t onboard the XAML train like many of my peers. [Note: I am now, but that’s for a different blog post]
So I took some time to create some snippets around Xamarin.Forms code generation and that still exist today. I have them available as pure code as well as for Resharper over at https://github.com/glennstephens/xfTemplates.
In addition to that, I do love using F# and using Fabulous is an amazing framework that demonstrated that Model View Update (MVU) is a workable way to create mobile apps. You can find the F# snippets over at https://github.com/glennstephens/fabsnippets. I’d also encourage you to check out Fabulous if only to understand the MVU pattern.
And I’m going to the same thing again this week. I like working with Xamarin.Forms and I especially liked working with Fabulous as I like the effectiveness of the MVU pattern and its design-time features. The next personal project I am going to make will be built using MVU. Right now I’m working with James Clancy’s Comet o get some simple proof of concepts running, but will eventually move to MAUI as it becomes more available.
So I’m going to create the snippets to do the basic controls and show you my process, although the snippets are using Visual Studio Code snippets as the Comet examples use Visual Studio Code. I’ve been working with Azure Functions with VS Code a lot lately and I must admit I’m enjoying the Visual Studio Code experience.
It’s a little early, and I’m going to apply the same aspects here that I do for all my snippets which for me are:
- Find the common things to do
- Make snippets for them
- Make a sample app to see the snippets work as expected and fast
So I created some simple snippets for Comet. The breakdown went something like the following:
- mvuState to create a state field
- mvuPage to make a new Xamarin.Forms page
- mvuTextSimple to make a label
- mvuTextFull to make a label with font size and color
- mvuTextField to make an editable text field
- mvuButtonSimple to make a simple button
Here is the snippet file for Visual Studio Code that allows me to perform these simple actions. I’ve linked it to a Github gist so I can update it later, but it will also give you an idea of the format that you can use for VSCode snippets, namely the JSON Format and then in the body, link the content in.
With all of those together, it took nearly no time at all to add a simple page which collected user data that I could do something with all while having a nice design-time experience.
When you do something a lot
The other section where you should make a snippet is where you code something a lot. There are some great examples of this in the Month of Xamarin. A lot of people work with Xamarin.Forms, RxUI, MVVM Frameworks or other components so it makes sense to simplify so that the process of creating a property for a ViewModel, for example, is about the same time as creating a variable. You can gain some pretty impressive productivity gains when you apply this principle.
I’d encourage everyone to explore the month of Xamarin code snippets over at https://luismts.com/code-snippetss-xamarin-month/ and see which snippets gel with you. The BindableProperty, XAML snippets and RxUI ones are the ones that suit me but explore and see what you can find out.
When you do something complex a little
Sometimes you do something complex occasionally but in a consistent manner. For Xamarin developers, one of the ones that come up for me is an Android Bound Service. They are complex beasts to make, the conventions are very strict and there are a number of classes that need to precisely define that should work together. They would typically need a Binder, a ServiceConnection and the Service itself to work.
For this to work I would add all of the files into a single snippet, ensure the classes link together and then use the light-bulb refactoring of moving the class to its own file once they were added to add them to their own files.
Here is the snippet to demonstrate the connectedness you can get. I use the $ServiceName$ parameter in order to fill in the type names of the multiple classes that are used.
Of course, you can apply this technique to many options such as in iOS if you are creating a UITableViewController with a UITableViewSource, or any collection of classes that require coupling between them.
One final thought
The last thing I’d like to mention is that whenever I complete a project I will do a post-project assessment. I’ll look at the code I write and see if that some of that code should be extracted to be in a library or if some of the code should be extracted to become a snippet. It's a habit I’ve found myself doing and I find taking a little bit of time can add some extra value.
Good luck and reach out over on Twitter at https://twitter.com/glenntstephens if you have any questions. Happy coding.