Does anyone change the Visual Studio default bracing style? - Is there a standard?

15,920

Solution 1

You need to have coding standards. There are no best standards. Standards such as having a brace on its own line or on the same line is a decision that one may take looking at comfort level of developers involved rather than on industry opinion (which will be typically divided).

But once standard is defined, you should adjust your tool to suit you. For example, you can change VS settings (Tools -> Option) as per your standards and then export those option groups as .vssettings file, keep it at central location/code repository and ask every dev to import it.

Solution 2

I'd stick with the default if you are working in a team and your code is under source control. If not you will end up having a hard time distinguishing the differences between real changes on check-ins and ones caused by the different position of the braces. In javascript from what I can remember there is good reason to use the braces as you have shown above. Douglas Crockford has a good article on why this is, I haven't added a link to the article because I couldn't find it right now, but it's worth having a look at his website none the less as it has heaps of interesting stuff on javascript.

Solution 3

I tend to stick to the default rules partly due to my own prefs but also because all projects I've taken part in (so far) has stuck to them.

You can find rules for new lines under Options/Text Editor/C#/Formatting/New Lines.

Solution 4

I used to fight this battle when I first started using c#. In the end I think 99% of people will use the default layout.

I bowed to the pressure of the crowd.

Solution 5

I always turn off the extra lines in the formatting settings, I like the more compact syntax.

I also change the font to Verdana, add a light gray background to string literals, remove bold on syntax pair matching (as it makes the code jump around), and set it to keep tabs instead of converting them to spaces. Other than that I find the default settings to work fine.

Share:
15,920
El Ronnoco
Author by

El Ronnoco

Awful programmer masquerading as somewhat competent programmer for 10+ years. Childhood 8-bitting GOTO-fan.

Updated on June 06, 2022

Comments

  • El Ronnoco
    El Ronnoco almost 2 years

    I find the default bracing style a bit wasteful on line count eg...

    function foo()
    {
        if (...)
        {
            ...
        }
        else
        {
            ...
        }
    }
    

    would, if I was writing in JavaScript for example be written like...

    function foo() {
        if (...) {
            ...
        } else {
            ...
        }
    }
    

    ...which I understand may also not be to peoples' tastes.

    But the question(s) is/are do you turn off the VS formatting style and use your own rules? What is the opinion of this in the industry when many people are working on the same code-base? Is it better just to stick to the default just for simplicity/uniformity?

  • Iain Galloway
    Iain Galloway over 13 years
    +1. Your company should define (or accept) a standard, and the tool should be adjusted to suit it. The one thing you don't want is two developers on the same team using different standards and wasting time correcting eachother. This is particularly a pain because it produces really noisy diffs.
  • El Ronnoco
    El Ronnoco over 13 years
    That's interesting, is it possible to have VS enforce pretty much any bracing style you like? For instance, the one I use in my second example, and then export this in a .vssettings file?
  • VinayC
    VinayC over 13 years
    @El Ronnoco, its possible - have a look at Tools->Options->Text Editor->C#->Formatting->New Lines->New Line Options for Braces. For import/export, check out Tools->Import/Export Settings.
  • El Ronnoco
    El Ronnoco over 13 years
    Yes, but it wouldn't be possible to force the } else { type structure I use above would it? The editor options would just allow you to switch 'else on a single line' on or off - and then it would be up to individual developers what they did?
  • El Ronnoco
    El Ronnoco over 13 years
    yes the tab->spaces thing I find annoying (reminds me of a somewhat arcane language I used to have to use). I might give your settings a go :)
  • VinayC
    VinayC over 13 years
    @El Ronnoco - your argument also applies even if you follow default bracing style. Yes - you cannot force developer to follow standard in the tool - at least while typing. But you can enforce it at later stages - manually by doing code review or at build/check-in time by using automated reviews - for example, VS Code Analysis/FxCop and StyleCop (stylecop.codeplex.com) - I believe the later should have way to catch bracing related non-conformance.
  • jrypkahauer
    jrypkahauer over 5 years
    Always follow your team's given code style guidelines. On one project we had StyleCop installed and enabled in the .sln, a non-standard rule-set, and "warnings as errors" enabled, and these were not only dictated to us, but checked into the repo that way. I hate the extra whitespace of normal .Net code, but I liked not having to worry about picking my style, too... we couldn't build without style-compliant code, which made sure that eveything was right as it went into the repo.
  • Tom Rutchik
    Tom Rutchik about 4 years
    This is the one answer that actually describes where you have to go to set your coding style, for C#. For javascript, that location is Tools/Options/Text Editor/JavaScript/TypeScript/Formatting/New Lines.