不同的括号形式背后的原因是什么?

时间:2022-07-14 22:26:33

I'm reading through the Zend Framework coding standards, where they state that curly brace after a Class definitions should be on the next line, the "one true brace form".

我正在阅读Zend框架的编码标准,在这些标准中,类定义之后的花括号应该在下一行,即“一个真正的花括号形式”。

class MyClass
{
    function....
}

I usually have the braces on the same line:

我通常在同一条线上有背带:

class OtherClass {
    function ...
}

What's the reason for putting the brace on the next line? Or using any other style, for that matter?

把支柱放在下一行的原因是什么?或者使用其他风格?

9 个解决方案

#1


7  

Personal preference is really the only real "reason".

个人偏好真的是唯一真正的“理性”。

#2


8  

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.

另外,在相同的缩进水平上使用括号可以让眼睛更容易找到匹配。

#3


7  

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.

我发现您提到的第一个样式有助于在视觉上从成员定义中抵消类名。这有助于我在扫描代码时更容易地找到类声明的顶部。

#4


3  

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.

就像其他人说的:如果你研究第三方代码,只要遵循它的惯例。如果您使用自己的代码,请使用您认为更好的任何样式。

#5


3  

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.

就我个人而言,我喜欢遵循Allman样式约定,因为它可以快速概述我的代码和缩进结构。当然,它会在代码中增加一些额外的行,但是我认为这并不能抵消优点。

Good resources on this matter are the following Wikipedia articles:

关于这个问题的好资源是以下*文章:

Indent Style

缩进风格

Programming Style

编程风格

Naming Conventions

命名约定

#6


1  

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.

用户的偏好。这真的没什么区别。当我在PHP中开发时,我使用了第二个选项,但是现在使用c#,我使用了第一个选项。

#7


1  

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.

有些人对此很感兴趣,我个人选择了调整大括号的“可读”选项(我不为屏幕上代码所使用的不动产付费……)所以我对紧凑性不感兴趣)但是当我用另一种风格为一个项目做贡献时,我只使用我贡献的那个。同样的标签大小,硬的还是软的,等等。

#8


0  

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.

标签和空格也是一样。

#9


0  

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

“我不知道。在凯文的笔迹。让我们去问问他。”

#1


7  

Personal preference is really the only real "reason".

个人偏好真的是唯一真正的“理性”。

#2


8  

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.

另外,在相同的缩进水平上使用括号可以让眼睛更容易找到匹配。

#3


7  

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.

我发现您提到的第一个样式有助于在视觉上从成员定义中抵消类名。这有助于我在扫描代码时更容易地找到类声明的顶部。

#4


3  

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.

就像其他人说的:如果你研究第三方代码,只要遵循它的惯例。如果您使用自己的代码,请使用您认为更好的任何样式。

#5


3  

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.

就我个人而言,我喜欢遵循Allman样式约定,因为它可以快速概述我的代码和缩进结构。当然,它会在代码中增加一些额外的行,但是我认为这并不能抵消优点。

Good resources on this matter are the following Wikipedia articles:

关于这个问题的好资源是以下*文章:

Indent Style

缩进风格

Programming Style

编程风格

Naming Conventions

命名约定

#6


1  

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.

用户的偏好。这真的没什么区别。当我在PHP中开发时,我使用了第二个选项,但是现在使用c#,我使用了第一个选项。

#7


1  

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.

有些人对此很感兴趣,我个人选择了调整大括号的“可读”选项(我不为屏幕上代码所使用的不动产付费……)所以我对紧凑性不感兴趣)但是当我用另一种风格为一个项目做贡献时,我只使用我贡献的那个。同样的标签大小,硬的还是软的,等等。

#8


0  

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.

标签和空格也是一样。

#9


0  

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

“我不知道。在凯文的笔迹。让我们去问问他。”