Category: Learn your IDE


Have you ever found yourself in the situation where you know what you want your program to do, but you just don’t know how to express it in code? If so, then read on. If not, read on anyway, as this still may be of use to you!

Code snippets are an excellent feature of visual studio, especially if you go to the effort of learning the shortcuts.

They most important shortcut to remember is Ctrl-K, Ctrl-X (multiple shortcuts used like this is known as a chord). This should bring a code snippets UI. Next I press V for Visual C# and then you can select or type the keyword of a load of constructs and code snippets will deal with the syntax for you. Any variables or names that you might want to define can then be tabbed through.

Hello, Code Snippets!

Here’s an example of a function created using snippets:

public void SortList(List<int> pValues)
{
  bool swaps = false;
  do
  {
    for (int i = 0; i < pValues.Count - 1; i++)
    {
      if (pValues[i] > pValues[i+1])
      {
        int temp = pValues[i];
        pValues[i] = pValues[i + 1];
        pValues[i+1] = pValues[i];
        swaps = true;
      }
    }
  } while (swaps);
}

And here are the parts of that code that were written using code snippets:


  do
  {
    for (int i = 0; i < pValues.Count - 1; i++)
    {
      if (pValues[i] > pValues[i+1])
      {

      }
    }
  } while (swaps);

Code snippets are particularly useful for developing in languages that you are not familiar with. Often you know what you want to do, but you don’t know the correct syntax. In these cases code snippets are invaluable. Here’s the same code, but this time written in Visual Basic, which I hardly ever use:

  Dim swaps As Boolean = False
  Do
    For index = 1 To pValues.Count - 1
      If pValues(index) > pValues(index + 1) Then
        Dim temp = pValues(index)
        pValues(index) = pValues(index + 1)
        pValues(index + 1) = pValues(index)
        swaps = True
      End If
    Next
  Loop While swaps

The logic is the same, but the syntax is different. If you’re ever unsure of the syntax you should be using to get something to work remember Ctrl X, Ctrl K and code snippets will help you out!

You can even define your own snippets, but I’ll save that for some other day.

Advertisements

To do lists are wonderful things. When you have a lot of things to do they help you to organise and prioritise all those things, they give you satisfaction when you cross something off your list, and they give you feedback on how you’re doing and whether you need to put some things on the back burner for a while.

Visual studio offers a built in task list to which you can add user tasks, assign priorities and tick off completed tasks.

User Task List – I wish my real to do list was this empty ๐Ÿ˜ฆ

There’s more though, the Task List will also scan your code for token comments – which are special comments containing specific strings.


// TODO: Refactor constructors to use constructor chaining

#region Constructors

#endregion

// HACK this works but it is bad practice to make this method public
 public int GetNextAccountNumber()
 {
return m_NextAccountNumber;
 }

By looking at the comments section you can see a TODO comment and a HACK comment that have been picked up automatically by the task list which read the comments in the code. Once the task is done the comment is deleted.

These tasks have been generated by reading comments in the code

You can even specify your own tokens. They say that you shouldn’t worry about making code more efficient until efficiency actually becomes a problem.

Adding a new INEFFICIENT token – it’s low priority though because efficiency isn’t an issue yet

You can add your own tokens by selecting Tools ->Options -> Environment -> Task List. Here I’ve created an INEFFICIENT token, so that if I write code that I know could be more efficient I can give it the INEFFICIENT token, and if I have to look for ways to speed up code later I can look for all the pieces of code that have been given this token.

Now if I ever want to speed up my code I’ll know just where to look!

During a recent and very insightful talk from Black Marble the speakers highlighted the importance of being familiar with your IDE.

This resulted in a bonus point for me and another blog post for you. This time though, I’m going to talk about a feature of an extension to Visual Studio called Power Productivity Tools. It’s free and supported by Microsoft. Power Productivity Tools is a customisable set of tools that is meant to make you more productive. It features all sorts of things but I’m going to talk about just one aspect of Power Productivity Tools, and that is the enhanced scrollbar.

The Enhanced Scrollbar - it's really useful

The enhanced scrollbar gives you an extra thick scrollbar with a visualisation of the entire code file embedded in it. It should you how your code is laid out, anything that is that same as the text that is currently select, open and closed regions, comments, errors (The are no errors in my code, at least not in the screen shot ๐Ÿ˜‰ ). I can even hover over code on the toolbar and have it pop up in a box next to the toolbar, right there in your window. It’s really useful for navigating code, finding out where a particular method is used, or just looking over your code to spot areas you might want to refactor.

It’s also just one part of the Power Productivity Tools extension. It’s probably the most obvious part, but there are a host of other features you can use, like a web link style Ctrl+Click function to jump the the definition of a method or variable, a more useful Solution Navigator and a whole host of options to change the way your tabs behave.

I thoroughly recommend checking it out.

There’s also another reason why it’s good to use extensions like this. Before Productivity Power Tools there were a host of similar add ins that people found useful. Now Microsoft has adopted these add ins as an extension, but chances are the most popular and most useful add ins may make it into future versions of visual studio as standard, so in some ways you’re future proofing yourself by learning this stuff now.

Of course there’s always the risk that your favourite tool won’t be adopted, leaving you bitter and twisted, and moaning about the good old days!