22

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?

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
  • Oops, didnt read question correctly. I pretty much like the default formatting, makes other people's code easy to prettify :) – leppie Jan 07 '11 at 10:01
  • 27
    @Moo, number of lines visible on screen at once is a precious resource. – Sedat Kapanoglu Jan 07 '11 at 10:02
  • @Moo/ssg Yes, this is what I mean, obviously in terms of storage it's nothing but it's just in terms of being able to get my routines on the screen. Coming from a VB background I'm used to getting more code content in fewer lines. (Cue response :) ) – El Ronnoco Jan 07 '11 at 10:04
  • @ssg, I think it makes things clear and looks less "cluttered". This is, of course, personal opinion :) – Moo-Juice Jan 07 '11 at 10:08
  • @Moo-Juice, that's a perfectly valid opinion. I'm used to the default style as well. – Sedat Kapanoglu Jan 07 '11 at 11:43
  • I _always_ turn off all the "open brace on a separate line" rules! I'll grant you that I am from the Java and JS world, so the whole "new line before opening curly brace" thing is very foreign to me and I just can't read the code very well... but the argument regarding vertical lines is very relevant... with the curlies on a separate line you lose 2 lines of vertical space, one before and one after... IMO picking up 2 lines for every curly brace is significant... – jrypkahauer Oct 10 '18 at 19:33

6 Answers6

23

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
VinayC
  • 47,395
  • 5
  • 59
  • 72
  • 3
    +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. – Iain Galloway Jan 07 '11 at 10:06
  • 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? – El Ronnoco Jan 07 '11 at 10:09
  • 12
    @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*. – VinayC Jan 07 '11 at 10:12
  • 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 Jan 07 '11 at 10:20
  • 1
    @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 (http://stylecop.codeplex.com/) - I believe the later should have way to catch bracing related non-conformance. – VinayC Jan 07 '11 at 11:30
  • 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. – jrypkahauer Oct 10 '18 at 19:39
5

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.

JasonFruit
  • 7,764
  • 5
  • 46
  • 61
Andrew
  • 5,215
  • 1
  • 23
  • 42
4

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.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
HSBallina
  • 424
  • 4
  • 15
  • 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. – Tom Rutchik Mar 29 '20 at 18:47
3

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.

Brett
  • 1,540
  • 9
  • 13
3

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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    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 :) – El Ronnoco Jan 07 '11 at 10:23
3

I don't think it is really about what VS default is, or even about what the industry standard is. The point that is most important is what does your team use, and what does the person paying the bills prefer. If your company doesn't actually have a set standard, you can look like a million bucks by suggesting that little gem. If source control is used, this standardization can save a LOT of noise when diffing things. This is however just my two cents.

Christopher B. Adkins
  • 3,499
  • 2
  • 26
  • 29