Friday, November 21, 2008
Ok, so Snippets are kinda neat

So my friend Bob says “We should look at the snippets thing in VS2005 for best practices and add our own snippets as we come up with a company specific set.’

And I say, ‘I don’t know much but I always worry that code gens are made by someone who knows even less’

Then I get to thinking that years ago Bob told me that ‘we should really take a look at Design Patterns to help us get things that have already been thought of rolled out faster’. To which I said ‘Just another repackaging of stuff we already know we should be doing but never have time to do right.’ Later, I read Head First Design Patterns and dang if Bob wasn’t onto something. Now I can read (basically) a DP catalog and actually (sometimes) get something really useful out of it.

So what of this stupid snippets thingie in VS2005?

Well, dang that guy Bob…. It actually is pretty darned cool. And if you do both VB and C# then as you’ll you see it’s pretty darned cool times two.

Here’s an example of how it works, or to be specific because it is important: How It Works In C#:

1) Stub out your method. This one takes a value (env) which is from an Enum (DSM.DBEnvironments) from a referenced dll

2)Right click and chose Insert Snippet from the context popup.

3) Scroll down to the switch and press enter

You get a default generic stub:

Notice that the argument is selected.

4) In that highlighted box, start typing the thing you want to do the conditional on, in this case, the “env” variable. Of course Intellisense helps you out.

5) Done with the variable typing? Click return.

And let VS add all of the options automatically. In this example, every one of the enum values is typed in for you and all you have to add is the callouts for each one of them. No need to go looking anything up or forcing AutoCompletes on line after line.

That’s neat, ‘specially if you need the occasional syntax reminder. It saved some time.

Now, look at the other side of the fence.

VB’s Snippet Engine works a little differently, it’s Task based rather than just smart-autocomplete based.

So, what d’ya wanna do today? Hit MSMQ?

1) Right click …

2) Click on Insert Snippet…

3) Pick a task type from the main list… MSMQ is a Windows kinda thing so click that.

Notice the breadcrumbs, you can click on any of these to walk your way back if you like (or just click anywhere on the editor and the Snippet popups go away).

But anyway, inside “Windows Operating System” is a folder “Message Queues”, click that…

4) I haven’t used MSMQ on a daily basis for a while, so remind me how to hit it… “Send and Receive” looks likely…

5) Click enter and all of the grunt skeleton work is done with required extra info areas highlighted.

Oh, and of if you’re wondering how that code could possibly work without namespace importing, take a look at the top of the editor and you see the System.Messaging namespace is automatically added:

… and before you point out that the Import/Using alone isn’t going to work without the reference being set to the external System.Messaging.Dll, take a look at the project References…

So C# has a smart syntax-of-common language-related structures and the VB snippet engine has a drill-down-to-task style to it (complete with breadcrumbs).

Imagine a new hire coming to you saying “How do I instantiate a MyCompanysComplexCustomer object?” and all you have to say is “right-click to the snippet popup and you’ll see it in the OurGreatCompany folder."

Yep, it can be done.

Snippets in VS2005. They really aren’t just collections of cut & pasties.

Darn you Bob. You were right again.


Related fun:

- Feb 2006 ::Code Magazine on newsstands now has a nice handy article on how to make your own and shareable code snippets, smart ones like the ones above. (That is a pretty nice magazine, by the way. One of the few that’s still written by and even managed by working Devs). At $6.00 it’s worth the price for lovers of print, but if you want a taste they also make every issue free online, here’s the direct link to the snippet article: http://www.code-magazine.com/Article.aspx?quickid=0601081

- The free (and pretty neat) VB Snippet Editor. The power of the Task-based hierarchical format also makes developing snippets for the VB2005 IDE a bit harder, so this tool gets you faster to market:

http://msdn.microsoft.com/vbasic/downloads/tools/snippeteditor

And the source code for the editor is on the MS GotDotNet site: http://www.gotdotnet.com/Workspaces/Workspace.aspx?id=a927f4e7-8e7f-45ce-8b72-f3b9384a3eab

Robert Smith
Kirkland, WA

added february 2006


Print  

pagecomment
  Add Comment



Submit Comment
  View Ratings
50.00%0
40.00%0
30.00%0
20.00%0
10.00%0

Number of Comments 0 , Average of Ratings
  View Comments
No comment.


Privacy Statement  |  Terms Of Use
Copyright 2008 by Robert C. Smith