imak
imak

Reputation: 6699

Code formatting styles - which one is preferable

So if I have a function like this in c# (#1)

private void Test(){

    }

and if you hit Ctl K D, it changes to (#2)

private void Test()
    {

    }

Two questions here.
1. Is there any hot key combination to convert the code from #2 to #1?
2. Is there any recommendation from Micorosoft which one should be used. I prefer #2 but a colleague of mine prefer #1 and he thinks this is what microsoft recommend but never gave me any links. I personally care more about readability of code then recommendation but just curious if there is any recommendation like this.

Upvotes: 1

Views: 818

Answers (6)

Washu
Washu

Reputation: 807

At the end of the day this is a personal preference... there are a few things to be sure of though:

  1. Be consistent, not just through a file but through an entire project.
  2. If you are working on a project with an existing code style then you should use that coding style yourself.
  3. You may not get to choose your coding style, depending on the project. Be flexible.

Upvotes: 1

Dave Mateer
Dave Mateer

Reputation: 17946

"Preferable" is, of course, subjective, but if all you are interested in is consistency, Microsoft's recommendations are codified in StyleCop, which will automatically check code for you for things like this (and much more). I think it uses style #2, but outdent the braces, for what it is worth.

Assuming this page is accurate, here are some internal style guidelines. Item 2.2 answers your question (they use style #2, but not with the extra indent on the braces)

Upvotes: 2

Joel Rondeau
Joel Rondeau

Reputation: 7586

To convert from #2 to #1, go to Tools->Options->Text Editor->C#->Formatting->New Lines, change the appropriate items (in this case, "Place open brace on new line for methods"), OK out of the dialog, and hit Ctrl K D again

Upvotes: 3

Spooks
Spooks

Reputation: 7177

Preference, and it will depend on your business programming structure(if there is one in place). I myself perfer (number 2)

private void Test()
{

}

You can usually change the IDE to format whichever way you want though.

Upvotes: 0

usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26874

Code conventions are sometimes subjective. You may find that a company strictly imposes code stylings that are different from other companies/people.

To answer: 1. No but CTRL-Z right after you pressed CTRL-K,D (but if you save and close the file, no way back) 2. I don't know about no publication from Microsoft about that subject, but they use that code styling as recommended by VS and in all their code fragments. So that should be the recommended version

By the way, I would prefer a space in sample 1: private void Test() {

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500893

Visual Studio comes with something like style #2 as the default, so that's what almost all C# code uses, in my experience. However, the braces in your example are indented for no obvious reason. More typical is:

private void Test()
{
    foreach (int x in bar)
    {
        Console.WriteLine(x);
    }
}

However, you can go into the Tools / Options / Languages / C# / Formatting and change it however you like. ReSharper gives even more options. Then you can reformat the code in your chosen style.

Upvotes: 2

Related Questions