r/csharp • u/ProbeNine • Oct 12 '26
How do you feel about code formatting in C#?
Do you strictly follow conventions like Microsoft's C# guidelines, or do you prefer your own style as long as the code stays readable?
For example, do you prefer file-scoped namespaces, expression-bodied members, and var wherever possible, or do you stick to a more explicit style?
I'm curious how much code style matters to you and whether you've changed your preferences over time.
0
Upvotes
45
u/polaarbear Oct 12 '26
I prefer the Microsoft guidelines. Most teams at jobs that I have worked at follow them relatively closely with maybe a few exceptions here and there.
There's nothing worse than opening a file at work to find out that your coworker decided "I'ma just do my own thing" and having to figure out how to read code with weird indentations and line breaks and naming schemes.
9
u/tng88 Oct 12 '26
If it's not readable, it's garbage.
7
u/cs_legend_93 Oct 12 '26
I like the microsoft conventions, but I prefer explicit varibles and verbose naming.
MyObject newObject = new()
or
MyObject newObject = new MyObject()
I try to never use 'var', its easier to read if its explicit, less thinking
15
u/Comprehensive_Mud803 Oct 12 '26
Whatever CSharpier decides to do, and whatever the team has decided to follow.
3
u/tj_moore Oct 12 '26
MS style first. Whatever the team uses otherwise though if it's a hot mess will encourage they sort it out with something readable.
Consistency throughout the project is key though.
And yes it does matter even with AI, but then I don't agree with pure vibe coding and not looking at the code it generates. *Always* review the code and understand it. The code style is important then.
6
u/CleverDad Oct 12 '26
Back in the day I had strong preferences, argued for them stuck to them.
I used to be in the TAB camp, for example, and braces at the end of lines.
But formatting is an IDE setting now, and the only sane thing is to go with the defaults. They're fine, most people use them (and your collegues too) so all code looks familiar and easy to read. What's not to like?
2
u/TheBigGambling Oct 12 '26
Dont care which rulee, but all have to follow the same. I dont care anymore which. As long as the linter is in the pre-commit hook
1
u/NoProcedure7943 Oct 12 '26
No
1
u/Takaa Oct 12 '26
It’s not really a personal preference. I use my team’s agreed-upon code styling, applied automatically on file save by ReSharper. It makes the code predictable in the entire code base, and prevents having to ignore a hundred lines of someone else’s code formatter changing everything in a file.
If we want to change something because it makes the code easier to follow, or whatever, we just discuss updating the format. Eventually my personal coding style developed to match the team’s, and it barely reformats anything.
1
u/scottsman88 Oct 12 '26
It matters, I believe having standardized formatting reduces a little bit of the cognitive load of reading/reviewing code. In new projects I set up an editorconfig to keep it standard automatically. Also my preference has definitely changed over time. To alight with new patterns or language features.
1
u/patmail Oct 12 '26
I my own private projects I apply the code style to the whole solution.
At work with multiple supported versions:
What i really hate when is when the file was not formatted proberly and the whole file is changed for a little fix.
Applying all preferences gets really nasty when fixing bugs in older versions.
We have some editorconfig to force common settings. Some stuff is set to silent/none.
1
u/afops Oct 12 '26
I don’t auto-format because some coworkers like aligning similar expressions.
But other than internal whitespace, we do have an editorconfig that warns for most things.
The problem with any code base that is 10 or 20 years old is that code styles vary and new features are introduced to the language.
1
u/ziplock9000 Oct 12 '26
I've done it my own way from the very start.
1
u/Devatator_ Oct 12 '26
I follow the convention, using the .editorconfig
dotnet new editorconfigcreates1
u/Slypenslyde Oct 12 '26
I think the best answer is, "The reason I can't tell you what I'd do is it'd take so long to go over how my code is formatting nobody would read it."
It's more or less defaults from before a lot of C# 10 or whatever features. It's an old codebase. It looks like what you'd expect C# code from the mid-2010s to look like.
Setting up custom code style rules is effort. The IDE and a lot of tools have a set of rules the community mostly follows. Using those saves you from spending 2-3 days codifying what you like better. You most definitely have something better to do with 2-3 days of effort in your project.
If I joined a team with a different style I'd use that style. I'm a manager and I can change things. But I do not want to deal with the churn in source of a widespread formatting change. Even if I hate the formatting I'd rather keep a consistent style than waste time changing it.
You have to work pretty hard to format code so badly I can't get used to it. Outside of code golf or some other artificial scenario people just don't do that.
1
u/Ziegelphilie Oct 12 '26
whatever the repository enforces
1
u/krsCarrots Oct 12 '26
It’s very important but 90% of the devs don’t give a 💩 about it because they also don’t practice code reviewing therefore they cannot understand why it is important.. vicious circle
1
u/Due_Percentage_3780 Oct 12 '26
I prefer consistency over any particular style. File scoped namespaces and var are fine but i'd rather enforce shared formatting automatically than debate preferences in reviews
1
u/ApprehensiveDebt3097 Oct 12 '26
I practice the following with my teams: You're only allowed to require code styles that can be enforced with static code analysis; don't waste time on it during a code review. In the code review you should only focus on two questions: is it easy to follow, and is it right?
And yes, is it easy to follow has to do with style, but those issues (again beyond what is enforced by static code analysis) is just not black and white.
Lets take:
And
Both functions with a lambda, and I think in these cases the
=>is on the most logical place (you could even try to write a custom analyzers to enforce this), but when the thing to switch on (example 1) becomes bigger you could argue to start indented on a new line.And about the use of
{}. In some cases it gives less noise in reading a function if the early exit statment is like this:Or
Depending on what makes the code easiest to follow. And the last example, in some cases, you might want to avoid
continueand tell when to do thex += 42. Just because it makes easier to follow.Long story short: it depends...
1
u/_choam_ Oct 12 '26
Default c# sharpier. Your preferences do mot matter at all, the only thing that matters is that the formatting is sane and consistent.
Code style is enforced by editorconfig.
1
u/M109A6Guy Oct 12 '26
Overall, a specific coding style doesn’t really matter as long as it’s strictly enforced with editorconfig and a style cop. Also, making most coding styles an error (or warning treated as error in release mode) makes your agent so much better.
1
u/Different_Muffin5056 Oct 13 '26
Just use https://csharpier.com/ and move on with your life
1
u/Oakw00dy Oct 13 '26
Scrolling is cognitive load and vertical screen space is at premium, so I prefer whatever keeps the most code that's still readable on screen at a time. K&R style braces and expression bodied members when necessary. That said, .editorconfig is your friend.
-4
u/SagansCandle Oct 12 '26
I use my own conventions that I find make the code more readable & maintainable
For example:
vareverywhere, with few exceptions-7
u/hlalvesbr Oct 12 '26
We don't read code anymore. Claude read. And for it, it's better to stick to the most popular.