-
-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nested statements without braces #255
Comments
I think that statements should always break + indent to be consistent. |
Don't see any issues per se with keeping everything on the same line. Emulating On a tangential note, I find allowing if/for statements without braces to be problematic in the first place and spitting in the face of consistency. Anyhoo, can't do much about that 🤷♀️ |
I personally prefer braces to always be there. However if an auto formatter would always break + indent for me, I think I might consider not using braces. If most c# developers that don't use braces on if statements always break + indent them, then I think it makes sense to follow that standard and not emulate prettier. I'm not clear how to really determine that. From my not very accurate search of the roslyn source, if statements without braces are a mix between breaking and non-breaking. I could theoretically write something that analayzes some public repos to figure out the actual stats. |
Just did some quick searches and reached the same conclusion as Bela. Both are used often, with the one using newline being more popular. However, this means that there isn't a clear consensus among the community and we should be free to choose an option solely on merit rather than being forced to maintain backwards compatibility with the community. |
Your regex search was definitely more accurate than mine. I'm not sure if you could get numbers using it, but I did tweak csharpier this morning to run some actual numbers. Although I just realized running the numbers like this is a little flawed. It doesn't take into account how long the lines are. For the IfStatements that break, are they something like
Or are they
Your search results do show that some of them are the later, but we don't really know how many. |
I guess I could get some hard numbers locally. Do you have a commit in |
No formatting is ever committed to csharpier-repos. An action runs against csharpier-repos to validate that csharpier won't throw exceptions or produce code that fails the validation check but the action doesn't commit anything. |
@belav Just searched the repo locally with Sublime Text and I arrived at an 80-20 ratio with the one having newlines taking the greater share. |
I often use an exit early strategy with statements without braces and I think that the readability get lost when no line breaks are present. Tested on my code and found some cases that are affected, especially the last case gets a bit weird. // before
if (evnt.Tenant?.IsSandboxed == true)
return;
// after
if (evnt.Tenant?.IsSandboxed == true) return;
// before
if (client.TheatreId == 0)
continue;
// after
if (client.TheatreId == 0) continue;
// before
if (maybeExisting != null)
client.TheatreId = maybeExisting.Id;
else
client.TheatreId = await theatreApi.Create(name);
// after
if (maybeExisting != null) client.TheatreId = maybeExisting.Id;
else
client.TheatreId = await theatreApi.Create(name); |
I'm inclined to say that we should break statements without braces. It seems like a majority of existing code does it that way. And it's also an easy fix for the nested statements problem. |
Currently statements that have optional braces will format onto a single line if they fit
If statements are nested, and still fit onto a single line they still don't break.
Should statements without braces always break + indent?
Should they break + indent if they contain another statement that could use braces but doesn't?
The text was updated successfully, but these errors were encountered: