Pablo Picasso
"Computers are useless. They can only give you answers."
My Latest Tweets
Tags

This will be shown to users with no Flash or Javascript.
Calendar
<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Sign in

So Visual Studio 2010 has this cool new feature called Reference Highlighting. If you don’t know anything about it yet, go and read the excellent Visual Studio Tips & Tricks blog to find out how to use it. What Zain Nabousi’s post doesn’t say however is that you can also customize the color of the highlighted symbols:

  1. Go to Tools > Options > Fonts and Colors.
  2. Select Text Editor in the Show settings for dropdown.
  3. Select Highlighted Reference from the Display items list.
  4. Change the item foreground and background to your preferred colors.

Reference Highlighting

Now when the cursor is place on a field for example, all references are highlighted as shown below and you can navigate between them using Ctrl + Shift + Down/Up Arrows.Visual Studio highlighted referenceNote that to remove the highlight, you need to move the cursor away from the field.

ReSharper has had a similar functionality since as far as I can remember - that is since at least version 3. To activate it press Shift + Alt + F11 and use Ctrl + Alt + Page Up/Down to move between the highlighted references. But, as usual, ReSharper goes one step further by highlighting where the symbol is being used (blue by default) or set (pink by default). ReSharper also places a coloured and clickable marker in the vertical margin that corresponds to the symbol’s usage within the file. To remove the highlighting simply Escape.ReSharper highlighted referenceThat’s it!

A lot of Developers who have never seen ReSharper in action often do not appreciate how much more productive they could be if they would start using it. Their arguments for not trying it out usually ranges from “I don't want to rely on a tool to do my coding” to “Why should I use R# when Visual Studio is already doing most of what I want/need” or “I don't like R# telling me what to do”. While these can be easily refuted, I think the main issue is that learning the tool itself can be a bit daunting at first. There are a lot of shortcuts to remember, options to set and functionality to digest. It also means adapting the way you work with the IDE and unlearn some old habits.

So if you are one of these reluctant Developers or just want to become a R# Jedi but don't know where to start, here is my advice: Learn the three shortcuts that rule them all... and the rest will follow!

1. Navigation

Navigation shortcut Navigation options

 

2. Code Generation

 Code generation shortcut Code generation options

3. Refactoring

 Refactoring shortcutRefactoring options

Note that all the options displayed are context sensitive and depend on where the caret is placed. Hence you will get different menu items based on whether your cursor is on a class, a method, etc.

I have been coding with ReSharper since version 3.0 and I simply can't work without it. It is IMO a must-have productivity tool and because of that I have encouraged any .NET Developer I work with to start exploring its capabilities by learning these three shortcuts. So far none of them have gone back to using plain vanilla Visual Studio. Hopefully if you break down the learning curve this way, you will also reap the benefits of what some people have called “the best IDE for Visual Studio”!

…Bonus Shortcuts: Go to type and Go to file.

Search shortcuts

The other day I grew increasingly frustrated with the amount of code I could or rather couldn’t see on my screen at work, even when working in full screen mode. I therefore wanted to write a Visual Studio macro to zoom in and out of the text editor. However while browsing through the sample macros that come with Visual Studio 2008, I found two macros that are doing just what I wanted: DecreaseTextEditorFontSize and IncreaseTextEditorFontSize. To check whether they are installed on your machine, simply go to Tools > Macros > Macro Explorer or type in ALT + F8 and then drill down to Macros > Accessibility.

Macro Explorer: Decrease Increase Text Editor Font Size 

Here is the Visual Basic code (after I changed fontSizeIncrement from 2 to 1):

' Set the font size increment and minimum font size
Private Const fontSizeIncrement As Integer = 1
Private Const minimumSupportedEditorSize As Integer = 3

' Increases the font size used within the editor.
Public Sub IncreaseTextEditorFontSize()
	Dim textEditorFontsAndColors As Properties

	textEditorFontsAndColors = DTE.Properties(“FontsAndColors”, “TextEditor”)
	textEditorFontsAndColors.Item(“FontSize”).Value += fontSizeIncrement
End Sub

' Decreases the font size used within the editor.
Public Sub DecreaseTextEditorFontSize()
	Dim textEditorFontsAndColors As Properties
	Dim fontSize As [Property]

	textEditorFontsAndColors = DTE.Properties(“FontsAndColors”, “TextEditor”)
	fontSize = textEditorFontsAndColors.Item(“FontSize”)
	If fontSize.Value >= minimumSupportedEditorSize Then
		fontSize.Value -= fontSizeIncrement
	End If
End Sub

If you don’t have the Visual Studio samples, follow the steps below to add these two macros:

  1. Open the Macro IDE (Tools > Macros > Macro IDE or ALT + F11).
  2. In Project Explorer, right-click on My Macros and select Add New Item…
  3. In the Add New Item… dialog, choose Module, give it a name (e.g. MyModule) and click Add.
  4. Simply copy and paste the code above in the newly added module.
  5. Save and close the Macro IDE.

 Add new module

All I had to do after that was to bind these macros to a keyboard shortcut. I chose ALT + [ to decrease the text font size and ALT + ] to increase it.

 Decrease text font size key binding

Now, I can easily zoom code in and out in the Visual Studio Text Editor.

In my previous post, I introduced NDepend and talked about how to get started. Now I’d like to review why I want to use it on my projects.

First of all, I really like the analysis reports. Not only do they show you instantly the current state of your application and how healthy it is (or not), but it also gives you an objective and systematic way of introspecting your code base. In conjunction with the code query constraints and metrics, they really help you monitor the quality of your .NET components. Even more so if you can run them as part of your build process.

As Mark Needham pointed out in this blog post, remembering to follow some of the OO design principles is not always an easy task when you’re coding. However by having the tool check for adherence to pre-defined rules/best practices, you can automate the identification step of the refactoring process thereby giving the Dev team more time to focus on the overall design, behaviour and functionality of the system.

Visual Studio already includes code analysis and FxCop but NDepend goes a lot further and one of its strength is that it let’s you visualize your code in a way that no other tool does. Napoleon said “un bon croquis vaut mieux qu’un long discours” (which more or less is the same as “a picture is worth a thousand words” in English) and so the diagrams produced by NDepend can help you digest complex architectures more easily.

NDepend Diagrams 

Now you might also ask what is wrong with just using the built-in Visual Studio 2008 code metrics? Well nothing but NDepend gives you much more than just cyclomatic complexity, depth of inheritance, class coupling and lines of code. There’s more than 80 code metrics out-of-the-box and if one is not applicable or you feel is inadequate for your particular application, you can easily customize the report by amending existing code queries or creating new ones to suit your needs. Also Visual Studio is not as granular and does not let you drill down the dependencies.

Note that NDepend has some rules defined around naming conventions but I think StyleCop is probably better suited to enforce a specific coding style and ensure consistency. Nonetheless NDepend includes some useful additional constraints such as the ones setting limits on the length of method or field names. Moreover it is relatively easier to add your own custom naming conventions for your particular project with NDepend.

What initially sparked my interest in NDepend is that it really complements ReSharper as a refactoring tool. In particular there are a few key design concepts that ReSharper’s code analysis simply does not cover, yet can be evaluated using NDepend: cyclomatic complexity, number of method parameters, class cohesion, total coupling and inheritance depth to name just a few. But with both tools in my arsenal I feel better equipped to write cleaner and more SOLID code.

I probably sound like an NDepend fanboy but I am passionate about using the right tools for delivering quality software. Yet NDepend can still improve in some areas and I have put below a wish list of the things that IMO would help make the tool even better:

  • Better integration with Visual Studio as I don’t like to leave my IDE and flip between the two all the time.
  • Code duplication analysis to reduce code clutter and abide by the DRY principle. Note that some of my colleagues have used Simian in the past to do this but I would personally prefer to do this with a tool I’m already using and familiar with.
  • Full screen mode for graph and dependency matrix windows.
  • Easier navigation between views and better shortcut support. There’s no shortcut for example to close an active window or to cycle through open windows (similar to Ctrl + Tab in visual Studio).
  • Automatic filtering of test projects when adding assemblies from a Visual Studio solution.
  • Being able to delete NDepend projects from the start-up page as at the moment you have to mess around with xml files to do that instead.
  • Quick search textbox in the Class Browser window similar to the one found in Visual Studio so that I can quickly go to a known type rather than drill down the treeview.
  • Cheaper single user licenses as I think the current pricing model is a barrier to entry for most Developers.

Finally I’d love to see some sort of integration with ReSharper. It would be great to be able to define code queries using NDepend and have ReSharper flag them as solution errors if the constraints are not met. This is probably fiction at the moment but one can dream!

Overall NDepend is a terrific product to pick up. It should save time over the lifecycle of a project, facilitate communication and increase productivity by aiding refactoring and generally making maintenance tasks easier to perform. Did I mention that it is also a cool way of learning about code metrics and some fundamental OO design principles?

I haven’t fully grokked NDepend yet but practice makes perfect and I’m really looking forward to use it on my next project. With the upcoming release of Visual Studio 2010, StyleCop + FxCop + ReSharper + NDepend will be a killer combination for developing .NET apps.

Disclaimer: This evaluation of Visual NDepend is based on a copy of the Professional edition supplied by Patrick Smacchia.

Caring about quality is not just about producing great code but it is also about producing great software.

NDepend is a static code analysis tool that offers some really unique features to help you improve the quality of your .NET applications. It can be used for conducting code reviews, highlighting areas that should be refactored, finding out dependencies and visualizing a complex code base. Ultimately NDepend aims to assist Developers in making more manageable and maintainable software.

NDepend is one of these rare tools I wanted to use for a long time but always found the limitations of the trial version too frustrating to live with. Now that I have a working copy of the professional edition, courtesy of NDepend creator Patrick Smacchia, I have been spending some more time with it, learning how it can help me write clean code along the way.

You only have to google “ndepend” and follow some of the links to find out how well regarded NDepend is within the .NET community. So what surprises me is that its adoption does not seem as wide spread as something like ReSharper, a tool which also aims at increasing code quality and Developer productivity. So why is it that a product with so many rave reviews has not gained the same traction as that of other coding tools out there?

Probably part of the answer lies in the perceived complexity of NDepend. It is indeed an intimidating tool and as soon as you open the start-up screen you are left wondering what to do next. This is not a tool that can be tamed in 10 minutes. It does require a certain amount of time, effort and experimentation to start enjoying its benefits. So where should you start to make that learning curve a little bit more gentle?

The amount of information provided by NDepend can be quite overwhelming at first and therefore a good starting point is to simply generate a report for a small project you’ve been working on. Using a code base that is familiar and relatively simple to follow will help you get a rough idea of the kind of information NDepend can provide without getting too bogged down into all the details. You can follow the steps below to quickly generate a report (I’m using the BlogEngine.NET source code here):

1. First build the Visual Studio solution for the application you want to analyse in debug mode - NDepend uses both the dll and pdb files (for linking back to the source code) to carry out the code analysis.

2. Open NDepend and click on ‘Create Project’ on the Start Page.

NDepend Create Project

3. In the ‘New NDepend Project’ dialog, enter name and location, and OK:

NDepend New Project

Once the new NDepend project is created, the ‘Project Properties’ window will be displayed.

4. Select the Visual Studio solution you want to analyse:

NDepend Add Assemblies

5. The loaded assemblies should now be displayed:

NDepend Loaded Assemblies

6. Either press F5 or click on NDepend Run Button to run the analysis report.

After a few seconds, the report will automatically be displayed in your browser.

NDepend Report

Once you’ve ran the report, you can use the VisualNDepend UI to browse your code.

VisualNDependNow you can either spend some time in the UI trying to work out what it all means or, preferably, head straight to the NDepend Web site and watch the online demos. There’s loads of documentation on the site showing you how to navigate the UI and to help you understand the information NDepend generates. Unfortunately the screencasts are not available for off-line viewing but they are however all relatively short (the longest one lasting no more than 5 minutes). After that you will be ready to explore and make sense of your code base armed with your newly found knowledge of code metrics.

There’s a couple of things I’d like to point out at this stage. First the NDepend install does not setup an entry in your start menu or a shortcut on your desktop so you might want to either create a shortcut for it or add it to your app launcher - I’m using Launchy for quickly accessing all my favourite applications. Second, by default the results of the CQL (Code Query Language) queries are evaluated automatically. I personally found this confusing at the beginning and you might want to turn that off to start with. To do that go to Tools > Options and uncheck "Execute automatically the query edited as soon as it compiles" as shown below. Once you’ve gained more confidence with the UI and how CQL works you will probably want to switch that option back on.

NDepend CQL Option

Finally, don’t dismiss the Info window took quickly. I initially ignored it without realising how useful it can be. Not only does it display detailed information about assemblies, types and members but it also gives you the description of dependencies in plain English; very convenient when you’re not familiar with the Dependency Matrix.

NDepend Info 

I won’t go into the details of what NDepend can do as there’s a well documented list of features on the site and other people have already done it better than I could - read NDepend: code metrics at your service for example. For more in-depth coverage of NDepend you can also follow Patrick Smacchia on CodeBetter.

Creating Extension Methods only takes a few steps with ReSharper :

1. Declare your intent

First tell ReSharper what you want to do. Note that in the line below, I have specified a local variable and an input parameter for the method to create. This will allow ReSharper to infer both the return type and the method parameter type.

Declare your intent  

 
 
2. Create static class

With your cursor on “StringExtensions”, press ALT + Enter (if you are using the Visual Studio shortcut keyboard bindings). In the Context Actions dialog, choose “Create static class” and press enter.

Create static class 

 ReSharper will generate the new class for you.

New StringExtensions class

 

3. Create method

Move your cursor to the method for which you want to generate the Extension Method. Open again the Context Actions dialog, select the first option and press enter.

Create method 

The method is automatically added to the new class.

New method

 

4. Static to Extension Method

Finally we need to convert our new static method to an Extension method. Highlight or set the cursor on the static method to convert, then open the “Refactor This” menu (Ctrl + Shift + R). Choose the last item to complete the process.

Convert Static to Extension Method   

ReSharper will not only refactor the method’s signature to add the ‘this’ keyword but also replace the original line to use the new Extension Method.

result

All you have to do now is to focus on the new method’s implementation.

Now in action:R# in Action: Creating Extension Methods Note: The screencast is heavily edited as my Dev machine simply can’t cope with Visual Studio and Camtasia running at the same time.

“Progress is made by lazy men looking for easier ways to do things”
Robert A. HeinleinW (American science-fiction writer, 1907-1988)

Let ReSharper do the work!

When I first started putting my blog together one of the first thing I wanted to do was to have a page where I could keep track of all the tools, utilities and frameworks I use or are simply installed on my Dev machine. It's nothing really original as there have been many such lists before with the best known being probably Scott Hanselman Ultimate Developer and Power Users Tool List for Windows. However this is one of these .NET sticky notes I wanted to put on the site.

With more than 150 items on the list, it's no wonder I hate rebuilding my Dev machine. It takes days if not weeks to tweak all of these applications back to the way I like it.

I have to confess I am completely reliant on all of those tools to do even simple tasks like cut and paste (I use CLCL for that). I am a compulsive R# user and I spend a rather unhealthy amount of time trying new applications, attempting to write scripts in AuthoHotkey or PowerShell, and pimping my IDE. My Visual Studio workspace is so heavily customized that I wake up in a cold sweat every time I need to reinstall it.

This heavy dependency is the most noticeable when pair programming, conducting code reviews or driving someone else's machine. I quickly lose track of what I am doing and feel totally paralysed. Fear takes over!

JP Boodhoo is right in saying that forcing Developers to work on standardized machine is counterproductive (The fallacy of the standardized developer machine). Unfortunately the reality is that if you work as a consultant moving from project to project, client to client, you might not necessarily have the choice. Many organisations have policies in place to lock down users' environment and that includes Software Developers. I even worked in some places where anything that you wanted installed on your computer had to be approved by a committee; the whole process could take months without guarantee of success! I also heard (rumours?) of Developers working in Delivery Centres who were prevented from accessing the Internet and therefore could not even do a quick Google lookup for some sample code or MSDN help. If it's true that would be plain cruel! Getting some employers to allow you to use a particular application or spend a few extra $$ on a tool (that could save loads of time and money in the long-run) is not always an easy task, yet they would paradoxically accept that Developers cannot write .NET applications in Notepad.

And this is the biggest downside of working with a mutant Dev machine. Going back to basics is almost impossible or fairly costly as you lose most of your productivity when working with an unfamiliar toolset or environment.

Yet I don't really want to go cold turkey and give up on all the goodies that help me deliver. So what is the solution for tools junkie like me? Share the knowledge of what is out there to make .NET Developers more productive in the hope that these applications will become de-facto standards in most organisations and demand that projects are given the tools that are essential to produce the quality of work that our customers and users deserve.

Until this happens, check out the Ultimate Tool ListThe Productive .NET Programmer list and some of the following sites:

Note: If you know of a good tool that could help improve a programmer's productivity and is missing from the list, let me know and feed a tools junkie's addiction.