gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:17:26 28 4
gpt4 key购买 nike

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




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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com