How to create code snippets using Snippet Designer?

I am working with my friend on creating a demo to show TDD. The whole demo took more than 30 minute, we were asked to reduce the time. When we write tests, we tend to write method names as long as possible so that when someone reads it, the name make perfect sense. So one thing I noticed, I spend most of the time typing large method names. One suggestion was not to type rather paste them and explain what the test does. Which led to the question, why not we use snippets? A good excuse for blog.

To start with go to snippet designer codeplex page and download the snippet designer. There is another way you can do it by creating XML etc., I like snippet designer very much and once you start using it, you may not go to XML format. Once it is installed, when you right click on the code, you should see an option to Export as Snippet

image

Select the option and export a snippet, it will open up a tab with the content you selected like the following

image

Two points are important here. Make sure you name the snippet (green circle) and then give name for shortcut (black circle). This is the name you are going to use to bring the snippet when you are in a code. Now save this file. There are two places you can save the snippet, one place is default visual studio snippet area. I will not save my snippet in there since it is specific to a project. If you are creating a snippet that you want to use a whole a lot, then you put it in Visual Studio directory. In my case I put it in the solution folder itself.  By default, snippet manager will show all the snippets stored in default visual studio directory so you need to tell snippet manager to look it up not only in visual studio default directory but also additional directories. To do that, go to tools –> snippet manager like the following

image

When you choose code snippet manager, it will show a dialog like the following

image

Change the language to C# and select Visual C# and select add to add the directory where you placed your snippets. Now that all pieces in place, go to your program and start typing ‘finalkata’ (Remember, this is the short cut name we gave?), as you type the intellisense should find your snippet and show up.

image

select it and double tab and bring the code. That’s about it. Very simple and easy to use. What I did not show is how to create the replaceable content inside the snippet, for what I did, it was not required. When I come around to use it, I will do another blog.

Naming Conventions in C#

Last week I had a chance to talk to one of my friend about the naming convention they are using for their projects. He went to on to great details on how they have standards in naming convention. In my opinion, developers shouldn’t think too much about standards before writing every single line of code. The standards should be natural for development which help maintenance rather than taking developers productivity down since they have to think twice before writing code.

One thing really stick with me was the rules they use for naming convention. They have very elaborate rules on class name, method name etc.,

I like the following simple rules for naming variables;

(Names should be meaningful and easily understandable.)

Use first letter capital for for all Methods/Properties.

Use _ as the first letter for private variables. I have seen people use m_ or my as prefix for private variables.

Use first letter lower case for all method parameters.

This goes with another idea of using properties instead of private variables. There was a discussion on this topic so I am not going to go into details.I prefer to use Properties rather than private variables. By making private variables with _, it will be easy to add a FxCop rule during file check in to see if anyone violated the rule easily.

Everyone uses different rules that suits their development.

Can you make dll disappear in Visual Studio?

This is more of note to myself so that I do not forget. I ran into a problem today. I am creating a simple console app, which is going to consume a dll my colleague wrote. Both are developed using VS 2010. One was developed using C# and another one developed using VB. C# main app targets 4.0 frame work, while the VB one targets 3.5 framework. References are added and started my first line of code.

VBNameSpace.VBClient client = new VBNameSpace.VBClient();

When I was writing, everything was good, Right after the namespace, VS promptly helped me with intellisense thus I know, dll is understood. I wrote just that first line and compiled the code and there it is, compilation failed with errors. The error message pertains to VBNameSpace that, it does not where to get it from. I thought, it may be that I missed something, so I deleted the reference and added the reference again and now in the VS studio all turned blue so I know VS understood the dll. I thought it might be my mistake, I might have been seen something. Anyway, compiled again, boom the error is back. I tried various things but nothing work.

Finally posted the question in stack over flow and got the answer (thanks to qntmfred). My mistake was in the project profile, my target framework was .Net Framework 4 client profile, that need to be changed to .Net Frame 4 (note, no ‘client profile’). With that change, it compiles and runs fine.

Technorati Tags: