strchr函数用法_c++ strchr函数_strchr函数原型(25)

电脑杂谈  发布时间:2017-02-28 18:06:59  来源:网络整理

bool retval = DoSomething(argument1,
                          argument2,
                          argument3,
                          argument4);
如果函数名非常长,以至于超过行最大长度,可以将所有参数独立成行:

if (...) {
  ...
  ...
  if (...) {
    DoSomethingThatRequiresALongFunctionName(
        very_long_argument1,  // 4 space indent
        argument2,
        argument3,
        argument4);
  }
条件语句:倾向于不在圆括号内使用空格.关键字else另起一行.

对基本条件语句有两种可以接受的格式.一种在圆括号和条件之间有空格,另一种没有.

最常见的是没有空格的格式.哪种都可以,但保持一致性.如果你是在修改一个文件,参考当前已有格式.如果是写新的代码,参考目录下或项目中其它文件.还在徘徊的话,就不要加空格了.

if (condition) {  // no spaces inside parentheses
  ...  // 2 space indent.
} else {  // The else goes on the same line as the closing brace.
  ...
}
如果你更喜欢在圆括号内部加空格:

if ( condition ) {  // spaces inside parentheses - rare
  ...  // 2 space indent.
} else {  // The else goes on the same line as the closing brace.
  ...
}
注意所有情况下if和左圆括号间都有个空格.右圆括号和左大括号之间也要有个空格:Warning:

if(condition)     // Bad - space missing after IF.
if (condition){   // Bad - space missing before {.
if(condition){    // Doubly bad.
if (condition) {  // Good - proper space after IF and before {.
如果能增强可读性,简短的条件语句允许写在同一行.只有当语句简单并且没有使用else子句时使用:

if (x == kFoo) return new Foo();
if (x == kBar) return new Bar();
如果语句有else分支则不允许:Warning:

// Not allowed - IF statement on one line when there is an ELSE clause
if (x) DoThis();
else DoThat();
通常,单行语句不需要使用大括号,如果你喜欢用也没问题;复杂的条件或循环语句用大括号可读性会更好.也有一些项目要求if必须总是使用大括号:

if (condition)
  DoSomething();  // 2 space indent.

if (condition) {
  DoSomething();  // 2 space indent.
}
但如果语句中某个if-else分支使用了大括号的话,其它分支也必须使用:


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-35033-25.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 张惟俭
      张惟俭

      中国发展迅速都是老百姓无形中的支持

    • 西楼公
      西楼公

      因为那是他们当年玩剩下的

    热点图片
    拼命载入中...