Joe White’s Blog

Life, .NET, and Cats

ReSharper, day 7: Code formatting

Welcome to day 7 of my 31 Days of ReSharper.

ReSharper has a great deal of support for code formatting. It basically falls into three categories: as-you-type, on-demand per file, and on-demand for multiple files.

Formatting as you type

This doesn’t sound new, does it? Visual Studio has had this for a while now. But of course ReSharper’s formatting options are versionable and sharable, so you won’t have some computers putting a space after typecasts and others removing it. This alone is a win.

Another nifty ReSharper formatting feature is in how it handles Enter inside empty curly braces. It’s a little better than the way Visual Studio handles the same thing, and ties nicely in with the automatic closing delimiters:

Starting out with an empty statement.
Type an open curly brace…
and press ENTER.

Formatting an entire file: Ctrl+Alt+F

ReSharper also has a command to reformat the entire source file you’re working on. If you press Ctrl+Alt+F, you’ll get the “Reformat Code” dialog box:

ReSharper's Reformat Code dialog box

Notice that it can do more than just tweak your whitespace. You can also tell it to fix some of those grays in your code. Optimize “using” directives” and Remove redundant ‘this.’ qualifier are pretty self-explanatory. Shorten references will change System.Drawing.Graphics to simply Graphics (adding the necessary using if needed).

We always check all of these boxes; we want our code as clean as it can get. It remembers your settings for next time.

Update: Maruis points out that Ctrl+Alt+Shift+F will do a silent reformat, without popping up the dialog, and just using whichever options you already had selected. Awesome! Thanks, Maruis!

Here are some other things ReSharper does when it reformats your code:

  • Adds explicit visibility specifiers. If you have classes with no visibility specifier, it will add the internal for you. Fields with no visibility get the private added. (As with everything in ReSharper, this is configurable. So if for some reason you wanted your code to be less readable, you could turn this option off.)

  • Puts simple getters and delegates on a single line. If the getter/setter/delegate body is a single statement (e.g., an assignment or a return), ReSharper will put it on a single line, along with the braces. So ReSharper would take this poorly-formatted code:

    public Color BorderColor {
            return _borderColor;
        set { _borderColor = value; Invalidate(); }

    and reformat it to look like this:

    public Color BorderColor
        get { return _borderColor; }
            _borderColor = value;

    Since Visual Studio 2005 doesn’t bother showing the extra layer of “collapse” plus signs for single-line getters and setters, this is a really nice feature.

Reformatting a project or solution

Finally, ReSharper has an option to reformat lots of files at once. Simply right-click on a folder, project, or solution, and select “Reformat Code”.

It may be obvious, but is worth noting anyway, that if you’ve made any other changes to your code, you should commit them to your source-control repository before you do this. Reformatting an entire solution is worthy of its own commit. It will change things, sometimes in ways you don’t want, and until you either change its settings, or adapt to its quirks, you’ll probably want the chance to selectively revert some of its changes.

Things it doesn’t get quite right #1: delegate indentation

By default, ReSharper indents your inline delegates a ridiculous amount. This is one of those where I think they got the default wrong: I want my code to follow normal indentation. I don’t want half of it ragged-center-aligned like this:

Block incrementer = delegate {
Block resetter = delegate {
                     i = 0;
                     j = 0;

I did finally find the way to fix this. The options for formatting anonymous methods are spread across two separate pages in ReSharper options: Braces Layout and Other. The “don’t indent a ridiculous amount” is on the Other page, and it’s called “Indent anonymous method body”. Turn it off, and set the brace option to “At end of line”, and you get something much nicer:

Block incrementer = delegate {
Block resetter = delegate {
    i = 0;
    j = 0;

Things it doesn’t get quite right #2: Xceed licensers

ReSharper tries to shorten qualifiers as much as it can. In suitably odd circumstances, it can decrease readability. Here’s one such case, and how we worked around it (which I think worked out quite well).

We use Xceed components in our software. One of our big frustrations with Xceed is the way they do licensing: they give you a license key, which is one of those ridiculously long strings of letters and numbers, and at runtime you have to write startup code that puts the key into a static property, like this:

Xceed.Grid.Licenser.LicenseKey = "LOTS-OF-LETTERS-AND-NUMBERS";
Xceed.Editors.Licenser.LicenseKey = "SOME-OTHER-LETTERS-AND-NUMBERS";

Yes, you have to set a license key for each Xceed assembly you use. Otherwise, some of them will complain at runtime. (Some of them, oddly enough, won’t, but we set their license keys anyway, just to be sure.)

The oddness is with a class named LicenseKey in multiple different assemblies. ReSharper tries to shorten that like this:

using Xceed;
using Xceed.Grid;
Licenser.LicenseKey = "LOTS-OF-LETTERS-AND-NUMBERS";
Editors.Licenser.LicenseKey = "SOME-OTHER-LETTERS-AND-NUMBERS";

Yuck. Here’s how we worked around it; ReSharper leaves this version alone:

using GridLicenser = Xceed.Grid.Licenser;
using EditorLicenser = Xceed.Editors.Licenser;
GridLicenser.LicenseKey = "LOTS-OF-LETTERS-AND-NUMBERS";
EditorLicenser.LicenseKey = "SOME-OTHER-LETTERS-AND-NUMBERS";

It’s a workaround for a tool limitation, but I think it actually reads better this way.

There are no responses to “ReSharper, day 7: Code formatting” yet.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Joe White's Blog copyright © 2004-2011. Portions of the site layout use Yahoo! YUI Reset, Fonts, and Grids.
Proudly powered by WordPress. Entries (RSS) and Comments (RSS). Privacy policy