Function Calls
On one line if it fits; otherwise, wrap arguments at the parenthesis.Function calls have the following format:
bool retval = DoSomething(argument1, argument2, argument3);
If the arguments do not all fit on one line, they should be broken up onto multiple lines, with each subsequent line aligned with the first argument. Do not add spaces after the open paren or before the close paren:
bool retval = DoSomething(averyveryveryverylongargument1,
argument2, argument3);
If the function has many arguments, consider having one per line if this makes the code more readable:
bool retval = DoSomething(argument1,
argument2,
argument3,
argument4);
If the function signature is so long that it cannot fit within the maximum line length, you may place all arguments on subsequent lines:
if (...) {
...
...
if (...) {
DoSomethingThatRequiresALongFunctionName(
very_long_argument1, // 4 space indent
argument2,
argument3,
argument4);
}
Conditionals
Prefer no spaces inside parentheses. Theelse
keyword belongs on a new line.
There are two acceptable formats for a basic conditional statement. One includes spaces between the parentheses and the condition, and one does not.
The most common form is without spaces. Either is fine, but be consistent. If you are modifying a file, use the format that is already present. If you are writing new code, use the format that the other files in that directory or project use. If in doubt and you have no personal preference, do not add the spaces.
if (condition) { // no spaces inside parentheses
... // 2 space indent.
} else { // The else goes on the same line as the closing brace.
...
}
If you prefer you may add spaces inside the parentheses:
if ( condition ) { // spaces inside parentheses - rare
... // 2 space indent.
} else { // The else goes on the same line as the closing brace.
...
}
Note that in all cases you must have a space between the
if
and the open parenthesis. You must also have
a space between the close parenthesis and the curly brace, if
you're using one.
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 {.
Short conditional statements may be written on one line if
this enhances readability. You may use this only when the
line is brief and the statement does not use the
else
clause.
if (x == kFoo) return new Foo();
if (x == kBar) return new Bar();
This is not allowed if the if statement has an
else
:
// Not allowed - IF statement on one line when there is an ELSE clause
if (x) DoThis();
else DoThat();
In general, curly braces are not required for single-line
statements, but they are allowed if you like them. Some
require that an if
must always always have an
accompanying brace.
if (condition)
DoSomething(); // 2 space indent.
if (condition) {
DoSomething(); // 2 space indent.
}
However, if one part of an if
-else
statement uses curly braces, the other part must too:
// Not allowed - curly on IF but not ELSE
if (condition) {
foo;
} else
bar;
// Not allowed - curly on ELSE but not IF
if (condition)
foo;
else {
bar;
}
// Curly braces around both IF and ELSE required because
// one of the clauses used braces.
if (condition) {
foo;
} else {
bar;
}
Loops and Switch Statements
Switch statements may use braces for blocks. Empty loop bodies should use{}
or continue
.
case
blocks in switch
statements can have
curly braces or not, depending on your preference. If you do
include curly braces they should be placed as shown below.
If not conditional on an enumerated value, switch statements
should always have a default
case (in the case of
an enumerated value, the compiler will warn you if any values
are not handled). If the default case should never execute,
simply
assert
:
switch (var) {
case 0: { // 2 space indent
... // 4 space indent
break;
}
case 1: {
...
break;
}
default: {
assert(false);
}
}
Empty loop bodies should use {}
or
continue
, but not a single semicolon.
while (condition) {
// Repeat test until it returns false.
}
for (int i = 0; i < kSomeNumber; ++i) {} // Good - empty body.
while (condition) continue; // Good - continue indicates no logic.
while (condition); // Bad - looks like part of do/while loop.
Pointer and Reference Expressions
No spaces around period or arrow. Pointer operators do not have trailing spaces.The following are examples of correctly-formatted pointer and reference expressions:
x = *p;
p = &x;
x = r.y;
x = r->y;
Note that:
- There are no spaces around the period or arrow when accessing a member.
- Pointer operators have no space after the
*
or&
.
When declaring a pointer variable or argument, you may place the asterisk adjacent to either the type or to the variable name:
// These are fine, space preceding.
char *c;
const string &str;
// These are fine, space following.
char* c; // but remember to do "char* c, *d, *e, ...;"!
const string& str;
char * c; // Bad - spaces on both sides of *
const string & str; // Bad - spaces on both sides of &
You should do this consistently within a single file or so when modifying an existing file, use the style in that file.
Boolean Expressions
When you have a boolean expression that is longer than the standard line length, be consistent in how you break up the lines.In this example, the logical AND operator is always at the end of the lines:
if (this_one_thing > this_other_thing &&
a_third_thing == a_fourth_thing &&
yet_another & last_one) {
...
}
Note that both of the &&
logical AND
operators are the end of the line. Feel free to insert extra
parentheses judiciously, because they can be very helpful in
increasing readability when used appropriately.
Return Values
Do not surround thereturn
expression with parentheses.
Return values should not have parentheses:
return x; // not return(x);
文章来源于领测软件测试网 https://www.ltesting.net/