Overview
A developer’s mind always finds some shortcuts to complete their work in some smarter way. Here we come up with one such smart solution, i.e., Code Snippet.
So, let’s start with the Code snippets.
What is a Code Snippet?
A Code Snippet is a term used for a little portion of reusable source code. It helps the developer in avoiding the typing of repetitive code for the same context.
Code Snippet Body Parts:
Name: It’s just a name for your snippet
Prefix: It’s a small word that will trigger your snippet in the editor. It looks like a macro.
Body: The actual code that gets copied in the editor when a snippet triggers.
Description: Sample details shown as a tooltip of the code snippet.
Create a Code Snippet:
Creating a code snippet is very easy. Create an XML file, add required elements, write your code and then save your file as “SnippetName.snippet”.
Code Snippet Template:
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title></Title> </Header> <Snippet> <Code Language=""> <![CDATA[]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
Let’s start writing our custom Code Snippet. Here, we have taken an example of a simple POST API code structure.
- Open the Notepad and copy above sample code into it.
- Set the title, author, description, and shortcut elements for the snippet.You can see this title in the Visual Studio’s Code Snippets Manager.
<Title>Title</Title> <Author>Author name</Author> <Description>Write your some basic description. It will be shown as tooltip.</Description> <Shortcut>postapi</Shortcut>
- Mention the language you want to use in the Code element. Here, we’re doing it for C#.
<Code Language="CSharp"> <![CDATA[]> </Code>
- Add the snippet code in <![CDATA[]>.
<Code Language="CSharp"> <![CDATA[ [HttpPost] public IActionResult API_Name([FromBody]$Request$ request) { try { dynamic result = new { }; // Write your logic here... return OkResponse(result); } catch(Exception ex) { return BadResponse(ex); } } ]]> </Code>
- Add Declaration and Imports elements as per your requirements. Declarations are used to set the cursor when the code snippet is copied to the code editor. Imports are used for importing the required namespace.
<Declarations> <Literal> <ID>Request</ID> <ToolTip>Add your request class name.</ToolTip> <Default>Request</Default> </Literal> </Declarations> <Imports> <Import> <Namespace>System</Namespace> </Import> </Imports>
Check the following image:
- Now, save the file with the “.snippet” extension. Our XML file is now ready for use.
Import Code Snippet:
- Open Visual Studio. Go to Tools > Code Snippet Manager.
- Click on Import.
- Select the folder location in which you have stored the XML snippet file.
- Open Code Snippet Manager and select the code snippet folder. Notice that in the right pan, the description, shortcut and author names are populated.The snippet file that we created will be copied at the following path:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC#\Snippets\1033\. Now, we can start using our imported code snippets. Write your Snippet Name Prefix and press Tab. The code will be copied to our code editor.
Sample Code Snippet.
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>postapi</Title> <Author>Yudiz Solutions Pvt. Ltd.</Author> <Description>Generates Post API structure for new implementations.</Description> <Shortcut>postapi</Shortcut> </Header> <Snippet> <Code Language="CSharp"> <![CDATA[ [HttpPost] public IActionResult API_Name([FromBody]$Request$ request) { try { dynamic result = new { }; // Write your logic here... return OkResponse(result); } catch(Exception ex) { return BadResponse(ex); } } ]]> </Code> <Declarations> <Literal> <ID>Request</ID> <ToolTip>Add your request class name.</ToolTip> <Default>Request</Default> </Literal> </Declarations> <Imports> <Import> <Namespace>System</Namespace> </Import> </Imports> </Snippet> </CodeSnippet> </CodeSnippets>
Advantages:
- Reduces the estimated time for code integration.
- Reduces the number of mistakes.
- Keeps the same format at all places, so the code looks much better and readable.
- Increases productivity.
- Gives rest to your hand and wrist. (They will thank you).
Conclusion
There are many other features that are helpful for coding. The code snippet is one of the most helpful features for every programming language. We can create custom code snippets as per our editor requirements. Write a bunch of code on just a few taps of fingertips, do clean and smart coding, keep the same format for the project.