开发者

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.


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


"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)

  • http://blogs.msdn.com/b/brada/archive/2005/01/26/361363.aspx


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.


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.


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() {


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜