Visual Studio quick tip: Use diff for any file

It’s possible to use the Visual Studio diff tool to compare any two files.

In the Command Window type in Tools.DiffFiles followed by the two filenames. You will see there is even an auto-completion feature.

DiffFiles.PNG

While external diff tools can be better than Visual Studio’s built-in, it has the advantage of being available everywhere you have a Visual Studio install (like on a client’s machine where you can’t install software, or a co-worker’s machine when you are giving him a helping hand).

It’s a very solid diff tool and Visual Studio users are already familiar with it from using it for merges.

SonarLint Visual Studio Extension / C# Linter

I’m currently working on a legacy code base that’s not a shining example of clean code and best practices.

I try to improve the code as I work, a careful balancing act where I do my best not to impact productivity by spending too much time on refactoring and reformatting.

To help me with this task I decided to look into linters. Note that I’m not using ReSharper which would help with code quality, but that’s another story.

I tried StyleCop, but with the default settings I just wasn’t impressed. Many rules are in my opinion needlessly restrictive. It also seemed to simply dump more than a thousand warnings in the error window.

I tried SonarLint next and I must say I was thoroughly impressed. Though the errors are also sent to the error window they are also shown inline in the file. The parsing by SonarLint was fast and running the Visual Studio extension only had a very small impact on performance even while working with a solution containing thousands of files and hundreds of thousands of lines of codes.

While I haven’t run ReSharper in years, performance was one of the two reasons I stopped using it. On large solutions like the one I’m currently working with, it would slow everything down to a crawl, even when disabling solution-wide analysis.

SonarLint isn’t exactly what I was expecting it to be. I thought it would be more for style guidelines but it has instead given me deeper insights. I’m specifically referring to my last two examples.

Things like needlessly nested if statements:

if (FirstProperty == false) 
{
    if (NeedlesslyDeeplyScoped == true)
    { }
}

Using a Count() instead of an Any:

if (files.Count(f => f.IsDeleted) > 0)
// to
if (files.Any(f => f.IsDeleted))

To foreach statements that can run on null variables:

List<Foo> myCollection = null;

if (IsWeatherRainy) 
{
    myCollection = new List<Foo>();
    // add stuff
}

// myColllection null on at least one execution path
foreach (var item in myCollection)

Or overlapping method signatures with optional parameters that can’t be used:

// This method's signature overlaps the second one
// and the default parameter value can't be used
public File GetFile(bool firstParam, 
                    bool secondParam = false)
{
    return new File();
}

public TestFile GetFile(bool firstParam)
{
    return new TestFile();
}

This last one having been found three times in a single file.

SonarLint is available on the Visual Studio marketplace. It’s free and open sourceĀ with a repository on GitHub. It’s created by a company that also sells a similar product (though geared towards CI / Build servers) but I haven’t found any downsides to using the free Visual Studio Extension.

By the way, I haven’t been asked to write this by the company that makes SonarLint.

Sniptaculous, a C# snippet library

Sniptaculous, a C# snippet library

I’ve just released the first version of Sniptaculous, a Visual Studio C# snippet library.

I think the existing snippets are great. They have parameters and some of them, such as the switch snippet (sw), will expand and make all of the switch cases for you if you use an existing enum as the parameter.

Since I love snippets and I find they speed up development, I’ve made some of my own. Over 50 snippets in fact.

I will continue adding to and refining Sniptaculous. I’m always open to suggestions and pull requests.

Disable mouse wheel zoom in Visual Studio

Today I’ve found a great extension for Visual Studio. I often hit the toggle that changes the mouse wheel from scrolling text to zooming text. For me that’s a really annoying interruption to my workflow and one of my Visual Studio pet peeve.

I tend to use the keyboard a lot with a bit of mouse on the side. This means one of my finger is often on or near the Ctrl key. The toggle is
`Ctrl + the scroll wheel` so I frequently end up hitting it.

Apparently I’m not the only one because someone made an extension to disable this very feature: Disable Mouse Wheel Zoom.

I can’t recommend it enough ;). There are other ways to change the zoom level and I really don’t need to toggle this feature (I’m scrolling and using Ctrl chords much more often than I need to change the text size).