Add null check without brackets in Visual Studio by Mark Seemann
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:
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:
This changes the behaviour of the Add null check Quick Action:
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
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 ) ) ).
When assigning parameters to fields, I like this one-line option for a null check:
How do you feel about it? You could even use a disard in your example:
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:
The compiler infers that
x
has the same type asthing
, e.g.Semigroup a => Maybe a
. Ifthing
, however, isNothing
, 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.
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.