A-K-
A-K-

Reputation: 73

How to prevent empty lines being places between single-line if statements?

In Rider (basically a standalone ReSharper for those who don't know), I can't figure out why automatic code formatting is placing an empty line between my if statements.

Before formatting:

string output = "";

if (i % 3 == 0) { output += "Fizz"; }
if (i % 5 == 0) { output += "Buzz"; }

if (output == "") { Console.WriteLine(i); } else { Console.WriteLine(output); }

After formatting:

string output = "";

if (i % 3 == 0) { output += "Fizz"; }

if (i % 5 == 0) { output += "Buzz"; }

if (output == "") { Console.WriteLine(i); } else { Console.WriteLine(output); }

I cannot figure out for the life of me the setting that is doing this, but it's quite irritating when you have multiple similar one-line if-statements grouped together and Rider/ReSharper displaces them all the time.

Upvotes: 1

Views: 1502

Answers (1)

Jonathon Chase
Jonathon Chase

Reputation: 9704

The setting you're looking for can be located by the following navigation: File -> Settings -> Editor -> Code Style -> C# -> Blank Lines

Under the subsection Blank Lines in Code, you're looking for After statements with child blocks. The reason this particular setting is adding lines in your case is the inclusion of braces.

if (i % 3 == 0) { output += "Fizz"; }
if (i % 5 == 0) { output += "Buzz"; }

could also be written as

if (i % 3 == 0) output += "Fizz";
if (i % 5 == 0) output += "Buzz";

The statements would no longer be considered to have child blocks, and therefore be unaffected. If keeping the braces is part of your desired style, you can set the value for After statements with child blocks to 0 and you'll get the formatting behavior you want when using single-line blocks.

Upvotes: 5

Related Questions