You’ve probably seen posts and comments on WordPress and other blogs that have an avatar associated with them. An avatar, in this case, is an image associated with an email address. You can get your very own avatar from Gravatar just by spending a few seconds signing up. From there, you can use one of their predesigned icons, or upload your own. Then, whenever you post something on a supported site, you’ll see your custom avatar… err, Gravatar.
One of the things I immediately notice when looking at someone else’s code, or my own for that matter, is the formatting. You know, indenting code blocks, space between methods, etc. The whole white space thing is something that I judge a piece of code on even before I’ve read it. If it’s typed in sloppily and looks unorganized, then the logic probably is too. Of course, this isn’t always the case, but for me, if it’s hard to read, it’s also hard to understand.
There’s a short-cut key in Visual Studio 2008 that performs this formatting for you. It won’t remove extra line breaks, you should still do that, but it will indent your code nicely. It works on code pages and ASPX pages, so there’s no reason to leave your code sloppy.
The key combination to format your code is Control K,D. Hold down the control key and type the letter K, followed by the letter D, then release the control key. Simple as that. No more excuses.
Here’s a screenshot of before…
And here is the same code after…
OK? So, no more excuses to be sloppy.
I’ve toyed with code snippets before, but they were a bit too complicated for me. I could never remember the shortcut keys or how to actually use or create them. I dismissed them long ago as too much of a hassle.
Visual Studio 2008 has an easier way to create and use blocks of code that I routinely find myself typing, by using the toolbox floating menu. I’ve really only ever used this for the visual controls and the server stuff. Now I store code blocks in a tab and use them frequently.
Here’s how to do it.
Make sure the toolbox is being displayed. If it’s not, click on View | Toolbox to show it. Pin it to the page so it won’t slide out. Now, right-click somewhere in the toolbox and select Add Tab. Name it whatever you want. I called mine ‘haXX code blocks’. Now, in your code editor, highlight a chunk of code and drag it to the new tab you just created. It’ll create a new entry for you with the first few words of your code as the title. Right-click on it and select rename to call it whatever you want. To use the new code block, just drag an entry from your code block tab onto the code editor page.
Warning… Unless you exit from Visual Studio cleanly (using File | Exit), you’ll lose everything you just created. If, like me, you connect to a machine using Terminal Server, and your session times out, you’ll lose it all. Ask me how I know. After you’ve made some massive changes, exit from Visual Studio, then start it back up again. There may be an easier way, but I haven’t found it.
You can do more than store code blocks this way. You can also drag a control from design view into the toolbox and save it for another day. This is all part of the whole ‘snippet’ programming paradigm. There’s specific syntax you can employ to highlight text, insert context-specific stuff, and all that. If you want to get fancy, you can. I don’t, so I’m not.