curly-braces - 括号放置会影响可读性吗?

标签 curly-braces

这个问题在这里已经有了答案:




12 年前关闭。




Possible Duplicates:
Formatting of if Statements
Is there a best coding style for identations (same line, next line)?
Best way to code stackoverflow style 'questions' / 'tags' rollover buttons


public void Method {

}

或者
public void Method
{

}

除了个人喜好之外,一种风格比另一种风格有什么好处吗?我曾经用第二种方法发誓,但现在在工作和个人项目中使用第一种风格。

通过可读性,我的意思是想象这些方法中的代码 - if/else 等......

最佳答案

Google C++ Style Guide建议

Return type on the same line as function name, parameters on the same line if they fit.

Functions look like this:

ReturnType ClassName::FunctionName(Type par_name1, Type par_name2) {
  DoSomething();
  ...
}


WebKit Coding Style Guidelines建议

Function definitions: place each brace on its own line.

Right:

int main()
{
    ...
}

Wrong:

int main() {
    ...
}

不过,他们建议将大括号用于其他所有内容。

GNU Coding Standards建议

It is important to put the open-brace that starts the body of a C function in column one, so that they will start a defun. Several tools look for open-braces in column one to find the beginnings of C functions. These tools will not work on code not formatted that way.

Avoid putting open-brace, open-parenthesis or open-bracket in column one when they are inside a function, so that they won't start a defun. The open-brace that starts a struct body can go in column one if you find it useful to treat that definition as a defun.

It is also important for function definitions to start the name of the function in column one. This helps people to search for function definitions, and may also help certain tools recognize them. Thus, using Standard C syntax, the format is this:

static char *
concat (char *s1, char *s2)
{
  ...
}

or, if you want to use traditional C syntax, format the definition like this:

static char *
concat (s1, s2)        /* Name starts in column one here */
     char *s1, *s2;
{                     /* Open brace in column one here */
  ...
}


如你所见,每个人都有自己的看法。就我个人而言,我更喜欢 Perl 风格的大括号在同一行除外-else ,不过只要大家在代码上能配合,真的无所谓。

关于curly-braces - 括号放置会影响可读性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/895326/

相关文章:

python - 在Python中转义单个 '{'

for-loop - 在Go for循环中打开大括号的语法规则

json - 需要转义json字符串对象内的花括号吗?

c - 在 C 字符串中转义大括号

javascript - 如何在大括号内的代码块周围使用 for 循环?

actionscript-3 - 成员变量/方法/getter 和 setter 可以包含在一个通用的访问修饰符中吗?

angularjs - 在页面初始化后评估 mustache 表达式(动态绑定(bind))

java - 哪种类型的花括号编码风格更适合 Java 程序员?

php - 包含内联大括号错误的 Smarty 模板

emacs - 在swank-clojure repl中模仿花括号匹配