Visual studio snippets

I only recently graduated from part-time coder to full-time coder. My previous job was part engineer, part programmer, so I never really spent enough time looking into some of the nicer features of Visual Studio.

Luckily, that’s all changed now. Snippets have by far become my favorite coding shortcut. At first, I didn’t think I had much use for it. If I had to create a long chunk of code I’d just revisit an older project and copy it over, no problems right? Well, now that I’m always coding I see what a waste of time that was.

I’m sure you can find out how to do these just about anywhere, in fact, I’m certain MSDN has a nice section on it, but I’m going to run you through it all the same.

Here’s my favorite snippet, it’s for an IConverter to be used in your xaml. These always have exactly the same starting point, so they’re perfect.

Step one is create a new file with the extension .snippet. In this case, I named it IConverter.snippet.

Now copy all the following code into it and I’ll go through it step by step. I’ve highlighted all the sections you’ll want to change.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>**Converter**</Title>
      <Author>**YourName**</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>**SnippetShortcut**</Shortcut>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>**type1**</ID>
                <Default>**double**</Default>
            </Literal>
            <Literal>
                <ID>**type2**</ID>
                <Default>**double**</Default>
            </Literal>
            <Literal>
                <ID>**class**/ID>
                <Default>**___Converter**</Default>
            </Literal>
        </Declarations>
        <Code Language="csharp"><![CDATA[**[ValueConversion(typeof($type1$), typeof($type2$))]
    public class $class$ : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is $type1$))
                return 0;


        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    **$end$]]>
            </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

OK, it’s pretty straightforward from here. Give the snippet a Name and Author, although they don’t really matter too much. More importantly, replace SnippetShortcut with something useful, in my case I used conv. This is the shortcut you’ll use in Visual Studio to run the snippet. All you need to do it type conv[TAB][TAB] and you’re up and running.

In the declarations section,

<Declarations>
    <Literal>
        <ID>**type1**</ID>
        <Default>**double**</Default>
    </Literal>
</Declarations>

You can declare all the variables in your code snippet, with an ID and Default Value. The ID can be used in the codeblock below. You have to surround the ID in $, like this $type1$ for the snipper to find and replace it correctly. The default value should be something sensible. I like to try and make that a useable value, so if I forget what that should be, the default gives me a hint.

Finally, replace the section that is highlighted bold (don’t delete the [CDATA[ or $end$ sections or it’ll break). Make sure to replace any section that might be a variable in your code block with the correct $variablename$.

OK, so you’re all done, now what? Well, open up Visual Studio, goto Tools —> Code Snippets Manager, click Add and locate your snippet folder. That’s it.

Now, after all that hard work you can relax and enjoy the beauty of code snippets.

Naming things is hard

There are only two hard things in Computer Science: cache invalidation and naming things.

— Phil Karlton

I thought this was a good way to get rolling on this blog. I’ve always been really bad at introducing myself (in real life too) so just go with it. If it helps, just picture someone standing up in a room full of people sweating profusely and mumbling while reading this.

Naming things is really hard. This isn’t just in relation to computer science either. My wife and I spent months picking out our kids names (I think we only decided on the 2nd kids name on his due date).

I spend (probably too much) time every day sitting in front of my computer trying to think of a good name for something. When I finally decide that it sounds good, I plow onwards until I reach my next naming problem. The really bad part is that you end up with a lot of code that looks like this –

UnitBuilder unitBuilder = new UnitBuilder();

The new name you come up with is so good, you end up using it for an instance and a class. I’m not going to throw away gold like that.

Naming this blog was hard too. I think it took me close to a week to even come up with one name. They were all terrible of course, I mean how do you name something that can be about anything to do with programming. I don’t know about you, but I think the best programming blog name has to be CodingHorror. My rejects included NeverendingCoding and StumblingThroughCode. I even tried this whole blogging thing once before and came up with the super clever name of EternallyRefactoring (I told my wife and she didn’t know what that meant, probably a red flag).

Somehow, I thought I’d found a good one that nobody else was using, CodeBlock.wordpress.com, but of course that was already taken, along with CodeBlog, AnotherCodeBlog and countless other “clever” ideas that I had. Anyway, here’s where I ended up. The idea for me is that you insert a breakpoint into your coding day to read my blog. Of course, if you’re a web dev you might be wondering what the hell a breakpoint is anyway.

Here’s a little about me. I’m a self-taught software developer. I didn’t get started until I was around 24, learning Java first and then moving into .NET with VB and C#. I’m now working full-time as a software developer in a small department of a large company, doing WPF and C# exclusively. I like to mess around with side projects in my spare time. I’ve developed a simple kids learning game for Android and iOS, and I have a couple of projects on GitHub that I’ve cobbled together when I find time.

This blog is probably going to be a stream of consciousness. Like I said, I tried this blogging thing once before and it didn’t really work out that well. I felt a lot of pressure to try and blog regularly and for all my blogs to be well researched. Not this time my friends. I’m going to be writing about things I’ve stumbled across at work or things I think people need to take a look at. That’s it. Very little research. If you’re looking for that, you’re in the wrong place bud.

Alright, thanks for stopping by!