4

Could anyone advise how to remove braces from any single-line statements? (Excluding the obvious just delete the braces manually)

Using C# in Visual Studio.

So instead of:

if (thingy is null)
{
    throw new ArgumentNullException(nameof(thingy));
}

Have the alternative:

if (thingy is null)
    throw new ArgumentNullException(nameof(thingy));

I have tried running CodeMaid and also changing the CodeCleanup (which just changes it back to having braces). I am happy to try any recommended Extensions etc to get this sorted.

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
craig157
  • 355
  • 1
  • 3
  • 18
  • 3
    You should be able to set this in your `.editorconfig` using `csharp_prefer_braces = false`: https://learn.microsoft.com/en-us/visualstudio/ide/editorconfig-language-conventions?view=vs-2019#code-block-preferences – canton7 Jan 15 '20 at 12:37
  • tried this earlier - but nothing seems to change. Had set this to 'when on multiple' rather than false, as I don't want the whole project to be affected. Am I missing how to run these preferences? – craig157 Jan 15 '20 at 12:44
  • 1
    Afaik this is bad coding practice and you should just not do it. Maybe just write it in one line, aka `if (tingy is null) {DoStuff();...}` – Honduriel Jan 15 '20 at 13:09
  • @Honduriel why do you think it's bad coding practice? – craig157 Jan 15 '20 at 14:59
  • 1
    It makes the code, at least in my (and my coworkers) opinion, harder to read and thus harder to maintain. – Honduriel Jan 16 '20 at 07:08
  • 2
    So a personal opinion rather than Bad Coding Practice... on the other end of the spectrum we find it easier to read as you can instantly identify it as a Singular Line statement due to the lack of braces, and anything with braces is greater than singular. – craig157 Jan 17 '20 at 08:43
  • 2
    It's a bug in VS, I faintly remember having it reported maybe a couple of years ago and nothing has changed since. Code Cleanup has an "Add/remove braces for single-line control statements" but it doesn't work. The config setting mentioned above only works in the opposite direction, it merely allows the situation without braces but doesn't enforce it. It should be reported again and again. Yes, I know, there is an urban legend of it being bad practice but it isn't, it's clearly a personal choice and the IDE shouldn't impose its own choice, especially if it promises the opposite. – Gábor Dec 06 '20 at 11:19
  • I reported it again and linked it to the earlier report that I also found: https://developercommunity2.visualstudio.com/t/Code-Cleanup-Addremove-braces-for-sing/1278877 – Gábor Dec 06 '20 at 11:44
  • @craig157 - Definitely not a personal opinion. Omitting the braces increases the potential for bugs. C# doesnt require indenting like python. Why would you want to do anything that could lead to a bug when you can easily avoid it? You are checking for nulls instead of letting them propagate and possibly cause harder to identify bugs. If you want brevity for guard statements, see the answer below for one-liners that wont need bracing – StingyJack Oct 02 '21 at 04:18

3 Answers3

5

If you are using Visual Studio 2019 Preview, then you can achieve your need in 2 simple steps.

enter image description here enter image description here

Chandraprakash
  • 773
  • 1
  • 10
  • 18
  • 1
    Unfortunately just on VS Community for now. But will check if these options are available. Thanks @Chandrapakash – craig157 Apr 20 '21 at 09:27
  • 1
    @craig157 This feature is available on all the versions of Visual studio 2019 including Community, Professional and Enterprise. – Chandraprakash Apr 22 '21 at 02:38
3

This is not a standard refactoring in Visual Studio. But there are extensions that add this.

Eg. Roslynator has its Remove Braces refactoring.

halfer
  • 19,824
  • 17
  • 99
  • 186
Richard
  • 106,783
  • 21
  • 203
  • 265
-6

You should not get into omitting braces on single line conditional as a habit. It is easy for someone (you or another person) to make a little mistake with it that creates a bug you have to deal with later.

Now I will get off my soapbox and share an even shorter null guard:

public void MyFunction(object thingy)
{
   _ = thingy ?? throw new ArgumentNullException(nameof(thingy));
   etc...

Nice and terse, and no risk of missing brace issues. For strings I will use an extension method to get the same one liner.

  public static string NullIfWhiteSpace(this string s)
  {
      return string.IsNullOrWhiteSpace(s) ? null : s;
  }

Then I can do:

public void MyFunction(string stringy)
{
   _ = stringy.NullIfWhiteSpace() ?? throw new ArgumentNullException(nameof(stringy));
   etc...

I do something similar for empty lists and dictionaries.

StingyJack
  • 19,041
  • 10
  • 63
  • 122