Coding Horror

programming and human factors

Code Snippets in VS.NET 2005

One of the most enjoyable new features in Visual Studio .NET 2005 is Code Snippets. This animated GIF illustrates how it works:

VS.NET 2005 code snippets in action

I'm demonstrating three types of snippets here:

  • simple expansion
  • template expansion (with variables)
  • surround

The easiest way to enter a code snippet is to begin typing part of the snippet shortcut name, eg "prop" for property expansion. Once you've typed enough to uniquely select the snippet shortcut name, press:

Tab,Tab (in C#)

?,Tab (in VB.NET)

.. and the snippet will appear. If it's a templated snippet:

Tab advances to the next field

Shift+Tab moves to the previous field

Enter completes the snippet and resumes editing at your previous cursor position

If you want to enclose code in a surrounding snippet (eg, you have code highlighted for a region), you must manually invoke snippet intellisense by pressing:

Ctrl+K, Ctrl+X

VB.NET's implementation of snippets is a bit more robust than the one in C#.

  1. VB.NET has a zillion snippets shipping in the box where C# has maybe.. two dozen?
  2. VB.NET supports code snippets that automatically add any necessary Import statements.
The VB IDE team has a helpful code snippets FAQ that covers the language differences in detail.

There's a snippet management UI you can invoke via the Tools, Code Snippets Management menu, or by pressing:

Ctrl+K, Ctrl+B

From here you can import and export snippet files, but there's no editor for creating new ones. Snippets are just XML files with a ".snippet" extension that live in these language specific folders:

  • c:Program FilesMicrosoft Visual Studio 8VBSnippets1033
  • c:Program FilesMicrosoft Visual Studio 8VC#snippets1033
Assuming you don't enjoy editing raw XML files, it might be easier to go with Microsoft's Snippy, a GUI for creating VB.NET and C# code snippets. There's also Michael Palermo's new website for sharing code snippets, although there don't seem to be many there at the moment.

Discussion

Media Center 2005 adds Non-Linear Stretch

As Omar recently pointed out, Update Rollup 2 for Windows XP Media Center 2005 adds one essential new feature that makes it a "must have" upgrade -- non-linear stretch! I took a few screenshots on my HTPC to illustrate.

This is typical 4:3 television content:

typical 4:3 television content in MCE 2005

On a widescreen 16:9 display, the content just doesn't fit-- at least not at the original aspect ratio. You get black bars on the side. So what can we do about this? You could zoom in:

zoom mode in MCE 2005

Look how much content we've cropped away around the edges! Let's try a simple stretch to fit instead:

stretch mode in MCE 2005

No content is lost, but we've distorted everything: it's wider and fatter.

Clearly zooming and stretching both have their drawbacks. Non-linear zoom, on the other hand, is a happy combination of both: the image is slightly cropped on the top and bottom, then progressively stretched more towards the edges; the center is barely stretched at all:

Intelligent Zoom in MCE 2005

Microsoft calls this Intelligent Zoom, and it has far fewer downsides than a pure zoom or stretch. It's my new favorite viewing mode for 4:3 content!

This new viewing mode isn't just cosmetic. It can be essential to preserving your plasma or LCD television investment! After 8 months, our plasma TV had noticeable burn-in on the edges where the "black bars" appear for the 4:3 content! I immediately switched the bars to 100% white until the wear levels equalized, and now I have the bars permanently set to 50% grey.

However, this new non-linear stretch mode is an even better option. Highly recommended.

Discussion

Pimp My IDE

I just updated my Programming Fonts entry. It now includes a much larger code sample in each font, and a few new fonts including the "gee, did I really just pay $100 for a single font" Pragmata. Be sure to check it out.

It's a shame that there's no CSS Zen Garden for IDE coloring and layout. I occasionally find amazing color schemes, such as this one from Leon Breedt's blog:

xeraph_code_colorization.png

White on black color schemes can be hard on the eyes, but this one has less contrast. It's more of a white on brown, with soothing related palette choices for keywords and strings. I'd actually consider using it. If only there was a site where I could browse more great looking IDE layouts like this one...

Well, we may not have that, but I'd like to see what IDE layouts you guys (and gals) are using -- if they are significantly different than the VS.NET default, naturally. Post URLs to screenshots in the comments. I'm mostly interested in the code editing panel, but feel free to show whatever you think is worth showing in the screenshot. Here's mine:

my_ide_vsnet2005_screenshot.png

With the screenshot, include a description of the rationale behind your decisions. Here are mine:

  • Because my research on code colorization showed very definitively that 100% contrast schemes are harder to read, I changed my background from white to 248, 248, 248.
  • I'm a big fan of RGB (red, green, blue) color schemes. Keywords are bright blue by default in the IDE; I modify that to the slightly darker Navy, and use Magenta for identifiers, and leave the comments at their default green.
  • If I wasn't using the Pragmata font, I would be using Proggy or Lucida Typewriter. See my programming font roundup for more.
  • I like using a background color for strings so spaces and other "non-characters" are easier to see.
  • I prefer a much brighter highlight for selected code than the boring old white-on-navy default.
  • I recently started highlighting numbers as well as strings. The jury is still out on this, but numbers tend to be significant in code (eg, Magic Numbers). It's been helpful so far.

But enough about me. Go ahead-- Pimp Your IDE! Post a link to a screenshot in the comments along with some explanation!

Discussion

Microchip Easter Eggs

Software engineers aren't the only ones planting secret easter eggs in their products.

Here's a fun article on the hidden art etched into microchips. There are a handful of images in the article, but many more at the Silicon Zoo website. Here's one I found of Mr. T on, appropriately enough, a T1 transceiver integrated circuit:

Microchip Mr. T

Notice the medallion says "T1". Who said hardware engineers didn't have a sense of humor?

If you want to see more of these hardware in-jokes, visit the Molecular Expressions: Silicon Zoo page and scroll down past the interactive Java tutorial image in the middle of the page. Direct links to the rest of the chip images start just below there.

Discussion

Wrangling ASP.NET Viewstate

Inspired by Scott Hanselman's recent post on ASP.NET viewstate wrangling, here's a roundup of tips for dealing with that ornery viewstate stuff. The first rule of thumb, of course, is to turn it off whenever you can. But sometimes you can't.

I think the DotNetNuke developers were the first to realize that moving the viewstate to the bottom of the page makes the page much more Google-friendly. Google only indexes the first (n) bytes of the page, so if you have 12 kb of Base64 encoded schmutz in your header, that isn't exactly helping people find your pages via Google searches. Scott Mitchell compares and contrasts several methods of moving the viewstate input field to the bottom of the HTML form.

The benefits from compressing viewstate are somewhat marginal, nowhere near what you'd get from zipping plain text. but it's still worth trying. Scott Hanselman points us to a nifty base page class that automagically compresses viewstate for you.

There's also an interesting article on eggheadcafe about moving viewstate to the server. Peter Bromberg benchmarks the performance difference between viewstate on the client, and viewstate on the server in Session, Application, or Cache.

ASP.NET 2.0 brings a few much-needed improvements to Viewstate:

  • Viewstate can be safely disabled without destroying intrinsic control behaviors thanks to a new container, Controlstate.
  • An improved formatter class, ObjectStateFormatter, appears to reduce ViewState size by half.

Here's to progress. Now pardon me while I crack my bullwhip. Hyaa!!

Discussion