Whats the reasoning behind the different brace forms? [closed]

大城市里の小女人 提交于 2019-12-05 08:39:36

Personal preference is really the only real "reason".

Having the braces on lines by themselves helps to visually separate the inner part from the outer part. That makes it easier to quickly scan through source code and distinguish blocks from their surroundings.

Plus, having the braces at the same indentation level makes it easier for the eye to find a match.

I find that the first style you mentioned helps visually offset the class name from its member definitions. This helps me find the top of the class declaration more easily when scanning code.

Often cited reasons are:

  • easier to match up opening and closing braces (for the first example)
  • don't waste an other line (so that you can fit more lines of code on the screen - the second example)

As others have said: if you work on 3rd party code, just follow its conventions. If you work on your own code, just use whichever style you find better.

I think coding style and naming conventions on personal or team projects are mostly a matter of personal taste (although it is wise a team adopts a single coding style and naming conventions).

Personally, I like to follow the Allman Style convention, as it gives me a quick overview of my code and indent structure. Sure, it will cost you some extra lines in your code, but I don't think that weighs up to the advantages.

Good resources on this matter are the following Wikipedia articles:

Indent Style

Programming Style

Naming Conventions

User preference. It really makes no difference. When I developed in PHP, I used the second option, but now using C#, I use the first.

There are already several topics on this highly subjective topic...

Some people are passionate about it, personally I chose the "readable" option of aligning braces (I don't pay for real estate used by my code on screen... so compactness doesn't interest me) but when I contribute to a project using another style, I just use the one around my contribution.
Same for tab size, hard vs. soft, etc.

The real reason is to have uniform looking code throughout the codebase. Both styles have their advantages, which style is "better" depends on personal preference and what has been used "traditionally" in the language and the project.

If a style of braces is prescribed, use that, else use what was being used before you arrived. If you're starting a new project use what is normally used in the language of choice.

Same goes for tabs vs. spaces.

Does it matter which brace style you use? No. Does it matter that everyone working on the same project or source file use the same brace style? No; not only does it not matter, but it's useful if brace styles differ from one coder to the next, even in the same file.

"Why exactly does this chunk of code do this?"

"I don't know. It's in Kevin's handwriting. Let's go ask him."

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!