Introduction
The problem with the majority of the snippets that I found is that none, absolutely none goes beyond the code generation! Sometimes I found very good snippets, well coded, but finish, nothing more. No generation of documentation, no #regions
directives! Yes, I know! Nobody likes documentation, but when you need to explain what the hell your class/method/etc. does to your buddy, or the same explanation needs to be re-made sometime after the code is ready, and you are working on another project, you'll remember the documentation that you did not make!
#regions
are another problem. Nobody likes the mix "snippets plus #regions
". Inside Visual Studio, #regions
enable blocks of code that have affinity to be visually put together, and with the collapse and expansion features, have the ability to focus on different levels of abstractions of the same code.
After this, I did decide to write a code snippet to a common task to every program's day: a property generation snippet. This snippet isn't a big one, far from this! This snippet aims only to show how even a small problem, when well solved can be of great help. ("this is not a big one", this snippet has up-to 46 lines. I've another snippet that generates a class inside your own namespace, with a custom exception class, eventlog helper class, performance counters, dispose methods, and documentation, that generates about 3.000 lines of code!!!)
Background
You can develop a snippet event with text editor or inside Visual Studio, but when you decide to delve in the code snippet world, the first smart thing to do is look for a snippet editor. In my experience, two free snippet editors are worth mentioning: "Snippet Editor" and "SnippetBuilder", the first one is what I use majority of the time.
When you develop snippets with a text editor, you'll need to work directly with the snippet's XML tag, which can be error prone. An editor will be of great help because this tends to hide the details. Provide some level of syntax coloring (very poor! but better than nothing!) and then publish the snippet. A snippet is basically a .xml file, but when you publish the snippet file, it is transformed into a .vsi that you or another person can import directly into Visual Studio only by double-clicking the .vsi file. And the most important feature: the editor is of great help when your snippet enables customization (replacements)! What is the ability to change the contents of the replacement variables with the user desired values at the moment the snippet is activated.
Using the Code
The following code shows the first part of the snippet:
#region _$field$ backing field
#region Documentation
#endregion Documentation
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private $type$ _$field$ = $defaultValue$;
#endregion _$field$ backing field
#region $property$ Fronting property
#region Documentation
#endregion Documentation
[XmlIgnore]
public $type$ $property$
{
[DebuggerStepThrough] get { return this._$field$;}
[DebuggerStepThrough] set { this._$field$ = value;}
}
#endregion $property$ Fronting property
$end$
This block generates a backing variable named: _$field$
of type $type$
with an init default value $defaultValue$
, and property named: $property$
of the same type. These replacement variables will be changed to the desired values, by example:
$field$
..............= workedDays
$type$
................= int
$defaultValue$
..= 0
$property$
.......= WorkedDays
It will produce this:
#region _workedDays backing field
#region Documentation
#endregion Documentation
[DebuggerBrowsable(DebuggerBrowsableState.Never)] private int _workedDays = 0;
#endregion _workedDays backing field
#region WorkedDays Fronting property
#region Documentation
#endregion Documentation
[XmlIgnore]
public int WorkedDays
{
[DebuggerStepThrough] get { return this._workedDays; }
[DebuggerStepThrough] set { this._workedDays = value; }
}
#endregion WorkedDays Fronting property
Points of Interest
Beyond the generated code, the documentation with references between the backing field and the property (see the XML tag <seealso>
!), and the #regions
generated are the main point here.
The documentation is almost ready, with a very little effort on summary
tag, this can be finished. Any one that comes to use this code will be able to see the IntelliSense showing the contents of the summary tag. And the #regions
will enable to expand/collapse the blocks of code to the desired level.