How to reasonably use spaces in a program

Source: Internet
Author: User

The following are the spaces used by Google engineers during programming:

1. Horizontal whitespace (horizontal white space)

The use of spaces in the horizontal direction depends on the specific position. Do not place spaces at the end of a line of code.

General:

Void F (bool B) {// There should always be a space before the braces

...

Int I = 0; // there is usually no space before the semicolon

Int X [] = {0}; // when initializing the array, the space in the brackets is

Int X [] = {0}; // optional. If you want to use spaces, put them on both sides of the element.

// In the inheritance and initialization list, space spaces should be added to the colon in inheritance and initializer lists on both sides of the colon.

Class FOO: Public Bar {

Public:

// Implement the function corresponding to inline, and place spaces in brackets and specific implementations.

Foo (int B): bar (), Baz _ (B) {}// no space in the empty parentheses

Void reset () {Baz _ = 0;} // separate parentheses and implementations with spaces

...

When merging Code, adding spaces at the end will bring additional work to others, because the code at the end should be removed. Therefore, do not add spaces at the end. If you have already added a file, delete it or delete it in a separate cleanup operation (preferably when no one else is using your file ).

Loops and conditionals (loop and condition)

If (B) {// Add a space after the keyword of the condition sentence and loop

} Else {// Add a space on both sides of else.

}

While (TEST) {}// there are usually no spaces in parentheses.

Switch (I ){

For (INT I = 0; I <5; ++ I ){

Switch (I) {// in

If (TEST) {// it is rare to add spaces in parentheses. If you want to use them, keep them consistent.

For (INT I = 0; I <5; ++ I ){

For (; I <5; ++ I) {// For loops usually have a space before and after the semicolon

For (Auto X: counts) {// a range-based for loop usually has a space before and after the colon

}

Switch (I ){

Case 1: // No space before the colon of case

...

Case 2: break; // Add a space after the colon, if there is code

Operators)

X = 0; // There are often spaces on both sides of the value assignment operator

X =-5; // there is no space between the unary operator and its parameters.

++ X;

If (X &&! Y)

...

V = W * x + y/z; // There are often spaces on both sides of the binary operator

V = W * x + y/z; // You can also delete spaces around factors.

V = W * (x + Z); // there is no space in the parentheses.

Templates and casts (Template and forced type conversion)

Vector <string> X; // in angle brackets

Y = static_cast <char *> (x); // No space (<and>), forced type conversion

//, There is no space between <front or>.

Vector <char *> X; // there can be spaces between the type and pointer, but they must be consistent

Set <list <string> X; // it can appear in C ++ 11 code.

Set <list <string> X; // C ++ 03 required a space in>.

Set <list <string> X; // You can also add a symmetric space before the list.


2 vertical whitespace (vertical white)

Link character minimize use of vertical whitespace.

Minimize vertical white space.

This is even more a rule: when you do not have to use blank rows, do not use blank rows. In particular, do not place blank rows that exceed one or two rows between two functions. Do not start with a blank row as the function and end with a blank row as the function. However, use blank rows in the function.

The most basic rule is: the more code displayed on the screen, the easier it is to track and understand the control flow of the program. Of course, the readability may also be affected by the code being too dense or too sparse, so you need to judge it by yourself. However, in general, vertical blank rows are minimized.

Some empirical rules can help determine when to use blank lines:

The blank lines at the beginning and end of a function have little help for readability.

Blank lines in the IF-else blockchain may be helpful for readability.

Http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Exceptions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.