Snip development time with snippets!
In development there is focus on cadence for delivery. We are going to cover how we can use snippets in Visual Studio to cut a bit of time down on tasks we repeat often while developing new features.
Code snippets is shortcuts you use to generate code. Like generating a class constructor or a property. For instance, by typing prop in your class, use the tab key to jump between variables for the snippet to fill in to complete the code section.
You can access this snippet feature from the Visual Studio | Tools menu and opening Code Snippets Manager. From there you can browse snippets you used before or upload your own.
Let discuss how we create our own snippet to improve our development productivity.
Snippets is xml files containing the code that will be generated when using the snippets. Here is a templated example of a Snippets file, defining a single snippet:
The <Header> section of the snippet contains the shortcut used and the information shown in the tooltip when making use of the snippet from Visual Studio. The shortcut must be unique and not conflict with already existing snippets.
The <Snippet> section contains variables we use in our code section as declarations. These variables will highlight for the user to fill in as they use the tab key in the code and fill in the default value as defined. The variables are optional and if your code section does not need it, you do not need to include it.
The code element within the snippet is the code that will generate when making use of the snippet in Visual Studio.
Below is an example of a snippet for creating properties that include the on notify property changed event (OnPropertyChanged) we make use of in application that is making use of MVVM:
Uploading the above snippet in the snippet manager will allow us to access the generated code section by typing promv and making use of the tab key to navigate the variables and define them. The result is a full property with a private variable and include the OnPropertyChanged() event needed for MVVM.
So, what is some more use cases for snippets?
We can use snippets to generate code that performs like a copy and paste, and then rename the values in it. When taking those code sections and making use of variables for the name changes, we can make use of snippets to generate the code for us.
We can use snippets for components that has the same constructor and methods, overridden, to generate the code that we know is normally used when defining a class.
We use snippets to generate code sections when doing a presentations to other developers, instead of copying code from a notepad.
Enjoy creating your own snippets and improving your development time by avoiding typing up repeating code. The three examples above are available on GitHub:
This article was written by Riaan Swart from the Innovation and IoT team in First Technology Digital. Riaan is a technical lead at First Digital and share his passion for coding with other software developers in the First Digital Innovation Team.