April, 2008

  • Kirill Osenkov

    More blogging goodness from C# IDE QA

    • 0 Comments

    I'm very happy to announce that two of my team members have started blogging.

    Eric Maino (http://blogs.msdn.com/eric) is our infrastructure expert and, as Charlie puts it quite correctly, one of QA's finest engineers. Also, Eric has a misfortune of having been appointed as my own personal dedicated mentor, which means he answers all sorts of lame questions I come up with and otherwise helps me to ramp up as a new Microsoftie. He's doing a terrific job. If you remember Mr. Wolfe from Pulp Fiction, he is the guy who solves problems quickly and effectively whenever you're in despair :)

    Daniel Rathbone (http://blogs.msdn.com/rathblog) is another great peer of mine on the team, known for organized way of thinking and working. Besides organizing information around himself, Dan also organizes the Weekly-Friday-4:30-PM-Visual-Studio-Languages-Drink-Of-The-Week, where the Managed Languages folk gets to taste various kinds of alcohol from all around the planet. And no, we don't write production code after or during Drink of the Week.

     

    P.S. When you read Eric's post about Disposing your Finalizers, I want you all to know, that I was the one who wrote the bad code. There, I admit it. Poor Eric spent a lot of time debugging before he found the problem that I caused.

     

    I will understand, learn and use the Dispose pattern correctly.

    I will understand, learn and use the Dispose pattern correctly.

    I will understand, learn and use the Dispose pattern correctly.

    I will understand, learn and use the Dispose pattern correctly.

    I will understand, learn and use the Dispose pattern correctly.

    I will understand, learn and use the Dispose pattern correctly.

    I will understand, learn and use the Dispose pattern correctly.

    I will understand, learn and use the Dispose pattern correctly.

    ...

  • Kirill Osenkov

    Book review: Jon Skeet's C# in Depth

    • 3 Comments

    If you share some of my interests (like writing clean code, or, even better, writing clean code in C#), you'll already know and love Jon Skeet's Coding Blog. If you enjoy Jon's blog as I do, then there's some really good news: Jon has actually written a book about C# called C# in Depth (http://csharpindepth.com). Manning publications was even kind enough to provide me with a free copy of the book, which is greatly appreciated.

    Now, this is not a usual C# book that just rewrites the spec in a human-readable form. C# in Depth is far from being a boring description of numeric types and for/while/do/break/continue constructs. It shows the most interesting and shiny aspects of the language and provides interesting background and insight on why the feature was introduced and what's the best way to use it. Moreover, Jon does a great job at comparing how to achieve the same results in C# 1.0, 2.0 and 3.0 - and I have to admit, the 3.0 version always looks aesthetically pleasing and way better than the earlier versions. Mastering lambdas, anonymous types & Co is something very important for developing a good programming style and the book will teach exactly that - how to write clean, elegant, beautiful code given all the power of the C# language.

    I really enjoyed the book and I highly recommend it. Thanks Jon!

  • Kirill Osenkov

    Coding productivity: macros, shortcuts and snippets

    • 19 Comments

    Visual Studio macros are a fantastic productivity booster, which is often under-estimated. It's so easy to record a macro for your repetitive action and then just play it back. Even better, map a macro to a keyboard shortcut. I'll share a couple of examples.

    InsertCurlies

    If you open up Visual Studio and type in this code:

    Void1

    How many keystrokes did you need? I've got 10 (including holding the Shift key once). It's because I have this macro mapped to Shift+Enter:

    Sub InsertCurlies()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.NewLine()
    End Sub

    So I just typed in void Foo() and hit Shift+Enter to insert a pair of curlies and place the cursor inside. Remarkably, I've noticed that with this macro I now almost never have to hit the curly brace keys on my keyboard. Readers from Germany will especially appreciate this macro, because on German keyboard layouts you have to press Right-Alt and the curly key, which really takes some time to get used to.

    This macro is also useful to convert an auto-property to a usual property: you select the semicolon and hit Shift+Enter:

    ConvertAutopropToProp1

    Try it out!

    ConvertFieldToAutoProp

    Suppose you have a field which you'd like to convert to an auto-implemented property:

    ConvertFieldToAutoprop1

    And when you click the menu item, you get:

    ConvertFieldToAutoprop2

    How did I do it? First, here's the macro:

        Sub ConvertFieldToAutoprop()
            DTE.ActiveDocument.Selection.StartOfLine( _
                vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
            DTE.ActiveDocument.Selection.EndOfLine(True)
    
            Dim fieldtext As String = DTE.ActiveDocument.Selection.Text
    
            If fieldtext.StartsWith("protected") _
                Or fieldtext.StartsWith("internal") _
                Or fieldtext.StartsWith("private") Then
                fieldtext = fieldtext.Replace("protected internal", "public")
                fieldtext = fieldtext.Replace("protected", "public")
                fieldtext = fieldtext.Replace("internal", "public")
                fieldtext = fieldtext.Replace("private", "public")
            ElseIf Not fieldtext.StartsWith("public") Then
                fieldtext = "public " + fieldtext
            End If
    
            fieldtext = fieldtext.Replace(";", " { get; set; }")
    
            DTE.ActiveDocument.Selection.Text = fieldtext
        End Sub
    

    And then just add the macro command to the refactor context menu or any other place. This may seem like no big deal, but I had to convert fields to auto-properties recently in 50+ files. I really learned to appreciate this macro.

    gs code snippet

    This is a very little but useful snippet: gs expands to { get; set; }

    <?xml version="1.0" encoding="utf-8" ?>
    <CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <CodeSnippet Format="1.0.0">
            <Header>
                <Title>gs</Title>
                <Shortcut>gs</Shortcut>
                <Description>Code snippet for { get; set; }</Description>
                <Author>Microsoft Corporation</Author>
                <SnippetTypes>
                    <SnippetType>Expansion</SnippetType>
                </SnippetTypes>
            </Header>
            <Snippet>
                <Code Language="csharp"><![CDATA[{ get; set; }$end$]]>
                </Code>
            </Snippet>
        </CodeSnippet>
    </CodeSnippets>

    Although I usually use the prop snippet to create auto-implemented properties, but gs is useful in some cases as well.

    I hope this has inspired you to do some personal usability research experiments and define everyday actions that you can optimize using macros, snippets and shortcuts. I would love to hear about your personal tips and tricks as well.

    kick it on DotNetKicks.com

Page 1 of 1 (3 items)