A Visual Studio tweak.

The most recent versions of Visual Studio have included many new Quick Actions, accessible with Ctrl + . or Alt + Enter. The overall feature has been around for some years, but the product group has been adding features at a good pace recently, it seems to me.

One feature has been around for at least a year: Add null check. In a default installation of Visual Studio, it looks like this:

Screen shot of the 'Add null check' Quick Action. By default it'll add brackets around the throw statement.

As the screen shot shows, it'll auto-generate a Guard Clause like this:

public bool Overlaps(Reservation other)
{
    if (other is null)
    {
        throw new ArgumentNullException(nameof(other));
    }
 
    var otherSeating = new Seating(SeatingDuration, other.Date);
    return Overlaps(otherSeating);
}

Part of my personal coding style is that I don't use brackets for one-liners. This is partially motivated by the desire to save vertical space, since I try to keep methods as small as possible. Some people worry that not using brackets for one-liners makes the code more vulnerable to defects, but I typically have automated regressions tests to keep an eye on correctness.

The above default behaviour of the Add null check Quick Action annoyed me because I had to manually remove the brackets. It's one of those things that annoy you a little, but not enough that you throw aside what you're doing to figure out if you can change the situation.

Until it annoyed me enough to investigate whether there was something I could do about it. It turns out to be easy to tweak the behaviour.

In Visual Studio 2019, go to Tools, Options, Text Editor, C#, Code Style, General and change the Prefer braces option to No:

Screen shot of Visual Studio Options dialog box.

This changes the behaviour of the Add null check Quick Action:

Screen shot of the 'Add null check' Quick Action after the behaviour change. It no longer adds brackets around the throw statement.

After applying the Quick Action, my code now looks like this:

public bool Overlaps(Reservation other)
{
    if (other is null)
        throw new ArgumentNullException(nameof(other));
 
    var otherSeating = new Seating(SeatingDuration, other.Date);
    return Overlaps(otherSeating);
}

This better fits my preference.


Comments

Daniel #

The same should go for if's, switch's, try/catch and perhaps a few other things. While we are at it, spacing is also a good source for changing layout in code, e.g: decimal.Round( amount * 1.0000m, provider.Getdecimals( currency ) ) ).

2020-09-07 7:14 UTC
James World #

When assigning parameters to fields, I like this one-line option for a null check:

_thing = thing ?? throw new ArgumentNullException(nameof(thing));
.

How do you feel about it? You could even use a disard in your example:

_ = thing ?? throw new ArgumentNullException(nameof(thing));

2020-09-07 13:14 UTC

James, thank you for writing. I'm aware of that option, but rarely use it in my examples. I try to write my example code in a way that they may also be helpful to Java developers, or programmers that use other C-based languages. Obviously, that concern doesn't apply if I discuss something specific to C#, but when I try to explain design patterns or low-level architecture, I try to stay away from language features exclusive to C#.

In other contexts, I might or might not use that language feature, but I admit that it jars me. I find that it's a 'clever' solution to something that's not much of a problem. The ?? operator is intended as a short-circuiting operator one can use to simplify variable assignment. Throwing on the right-hand side never assigns a value.

It's as though you would use the coalescing monoid to pick the left-hand side, but let the program fail on the right-hand side. In Haskell it would look like this:

x = thing <> undefined

The compiler infers that x has the same type as thing, e.g. Semigroup a => Maybe a. If thing, however, is Nothing, the expression crashes when evaluated.

While this compiles in Haskell, I consider it unidiomatic. It too blatantly advertises the existence of the so-called bottom value (⊥). We know it's there, but we prefer to pretend that it's not.

That line of reasoning is subjective, and if I find myself in a code base that uses ?? like you've shown, I'd try to fit in with that style. It doesn't annoy me that much, after all.

It's also shorter, I must admit that.

2020-09-08 05:57 UTC

The above default behaviour of the Add null check Quick Action annoyed me because I had to manually remove the brackets. It's one of those things that annoy you a little, but not enough that you throw aside what you're doing to figure out if you can change the situation.

Until it annoyed me enough to investigate whether there was something I could do about it. It turns out to be easy to tweak the behaviour.

In Visual Studio 2019, go to Tools, Options, Text Editor, C#, Code Style, General and change the Prefer braces option to No:

You can do more than that. As suggested by Visual Studio in your second screenshot, you can (after making the change to not prefer braces) generate an .editorconfig file containing your coding style settings. Visual Studio will prompt you to save the file alongside your solution. If you do so, then any developer that opens these files with Visual Studio with also have your setting to not prefer braces. I wrote a short post about EditorConfig that contains more information.

2020-09-13 01:53 UTC


Wish to comment?

You can add a comment to this post by sending me a pull request. Alternatively, you can discuss this post on Twitter or somewhere else with a permalink. Ping me with the link, and I may respond.

Published

Monday, 07 September 2020 06:47:00 UTC

Tags



"Our team wholeheartedly endorses Mark. His expert service provides tremendous value."
Hire me!
Published: Monday, 07 September 2020 06:47:00 UTC