C ++ code writing specifications

(Disclaimer: This blog page reference book from Jane https://www.jianshu.com/p/c1b6cf190e0b    following are reading combined with my programming experience to share my understanding and suggestions on the code specifications)

 First, the aforementioned

  Code as a tool to communicate with a computer programmer, and its importance is self-evident, but in the program a long way off, it is impossible has been a self-programming, group cooperation is essential. Therefore, the specification of the code thus it is very important, if you write a non-standard, gives a visual sense of ugly, then your partner would be difficult to modify the code you write, or even refuse to cooperate. Therefore, as the code for beginners to master the necessary knowledge is the most basic code standards, this blog as an example to c ++, for example analysis code specifications.

 Second, the case study

1. make good use of space and blank lines

  ① the proper use of spaces and blank lines to give people comfort, rather than a series of compact code gives the pressure.

  Do not leave a space after the function name, or parameter represents the obvious

  Such as: int Fun1 (int x) // write recommendation

    int Fun2 (int x) // do not recommend writing

 

  After ②if, for, while other judges and recycled to talk keyword should leave a space left parenthesis '(', to highlight the keywords.

  如: if (i = 0)

 

  ③ comma after the definition of a variable to spaces. Semicolon for the statement to be followed by spaces.

  Such as:

 for (i=0; i<5; i++)

      int a, b;

 

 

  ④ assignment operators, comparison operators, arithmetic operators, logic operators, bit field operators, such as "=", "+ =", "> =", "<=", "+", "*", " % "," && "," << "," ^ ", etc. should be added before and after the space.

  如: a = b; a + = 10;

 

  ⑤ When implementing a function, the function should be added between the blank line and functions to facilitate distinguishing between two different functions.

  Such as:

 void Fun1(int x)

  {

    //do something

  }

 

  void Fun2(double y)

  {

    //do something

  }

 

 

  PS: the excessive use of space nor a good code specifications, the appropriate use, such as inappropriate: for (int i = 0; i <10; i ++) gives very empty spaces abuse such feeling.

 

2. Note that the contents of each line of code

  One line of code to do one thing, not all features will be compressed to one line statement.

  Such as: 

int main () 

  { 

    int NUM; Double COUNT; the while (NUM <= 100 ) NUM ++; return  0 ;  // bad code specifications 

  } 

    int main () 

  { 

    int NUM; 

    Double COUNT; 

    the while (NUM <= 100 ) 

  { 

    NUM ++ ; 

  } 

    return  0 ; 

  }

 

 

3. variable, function names naming conventions

  ① variables, functions, file naming the best use of the English word or phrase, do not use pinyin, or meaningless abc and the like, with English words or abbreviations can play can play named to the reader in the absence of prompt comments effect.

  ② class and function names can be separated and ordinary variable region begin with a capital letter.

  Personal favorite use naming conventions: double hump nomenclature:

  Double hump nomenclature name, if the variable is when the composition is made by a plurality of words, the first word begins with a lowercase letter, a second, three, four first letter capitalized words ...... N. For example: myFirstWord, such as variable names look like a camel peak after another, not only beautiful, but also highlight the variable named by the phrase, reduce the possibility of conflict named.

 

4. Timely add a comment

  Sometimes reading the code will not keep thinking programmers, if the code is not next to the comment, it is difficult to understand its meaning, and therefore appropriate to add comments not only can make their own memories of a line, use a piece of code, but also aspects of the reader in understanding.

  It proposed to add a comment to explain the function of a function after each function name, and recommended that it's best to add comments in each cycle of the main function, so that readers understand the circulation.

 

These are my blog to read reference book review and recommendations in the hope that they can do well to remember these specifications, beautiful and practical writing code.

Guess you like

Origin www.cnblogs.com/besthunterhj/p/11455022.html