måndag 25 februari 2008

Visual Studio Code Snippets and Macros

In this first real post I'll try to explain something called macros in Visual Studio, and how they can make your work (programming) easier. I just recently found out about them, and realized that they resemble something I've been missing in the C++ environment of VS for a long time; code snippets! If you don't know what code snippets are, then here is a short description:

Code snippets are short keywords in your C# document, which when you double-press tab will be converted into C# code! If the code is parameterized, over for example different names or types, then these parameters will be selected first so that you can easily change them. If you for example write prop and double-press the tab key, a new property will be added to the class you are in.
prop <tab><tab>
becomes
private int myVar;

public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
where int, myVar and MyProperty are parameters which can easily be changed. There are a lot of nice code snippets to use in the C# environment, making coding easier. Just goto Tools -> Code Snippet Manager... or press Ctrl+K followed by Ctrl+B for a list of possible snippets.

But for C++ this doesn't seem to exist. I read something about code snippets being available for C++ in VS 2003, but somehow they where removed in VS 2005.

So this is where macros comes into play! With a macro you can more or less do what ever you want to do. You just create a macro, write the code for it (in VB) and then use it from the macro explorer within VS. To make it even more powerful you can bind keyboard shortcuts to these macros (Tools -> Options... -> Environment -> Keyboard). To show the currently available macros, activate the macro explorer using either Tools -> Macros -> Macro Explorer or by pressing Alt+F8. By default you will have a list of sample macros installed, which are great for playing around with and to get familiar with macros.

So, what then can you do with macros, you ask yourself. Well, for example you can write macros that automatically adds new classes, with a default header and cpp-file, based on a class name. You just tell VS to add two files to your current project, and then put some the class definition in the header, and some default implementations of perhaps constructors and destructors in your cpp-file. Or you can create a macro that cleans up headers and cpp-files, by searching in the code using regular expressions for #include "ClassName.h", and then comment this include if ClassName can't be found in the code. The possibilities are really endless!

So if you are developing in VS using C++, or actually any language supported by VS, and haven't used macros, then go ahead and try it out. It's amazing what you can do! Or if you are developing in C# and haven't tried code snippets then I really encourage you to learn about them. As have been said before: to be a good programmer, you must know your tools!

söndag 24 februari 2008

Free coffee with Gustaf and ideas of sharing knowledge

So, the idea of this blog is to try to share the knowledge I'm gaining when reading other blogs, listening to podcasts, discussing with friends over coffees, reading books or what ever. As mentioned in an episode of Hanselminutes: to become a better programmer you should try to write the knowledge down and share it with other people. So that's what I will try to do in this blog!

But not now, because it's too late :)