Software programming specifications summary

EDITORIAL:
This article aims to summarize the backup, to facilitate future inquiries, as is the personal summary, if wrong, please correct me; in addition, most of the content from the Internet, books, and various manuals, should please inform the infringement, immediately delete posts to apologize.

A copyright statement and version

Recently read a lot of the older demo program, generally have the following elements: file name, author, version information, date, summary, update log (Version History)

Copyright statement and version of the source file is located in .c, .h beginning of the header files, but only set a personal version and copyright statement in the source file because the file header are some statements, usually minor, and too much explanation seemed a bit cumbersome . (This depends on personal habits)

Second, the header file

Header files for only "the statement," Do not store the "definition", the definition of the source files are put inside each .c source file should have the same name .h header file; .c / .h files do not contain too much (that is, not to go in useless also included); prohibition cycle contains header files; should usually kept separate header and source files for maintenance and viewing.

Beginning necessary (to prevent the header file from being referenced)

#ifndef  __XXX_H
#define  __XXX_H                   //跟头文件名一样,全大写
 ………
#endif  /* __XXX_H */              //在后面备注是哪个#if的结尾

#Include and then to finish the above referenced library,
a reference library of standard header files #include <xxx.h> format, eg: #include <stdio.h>;
reference nonstandard library header files #include "xxx. h "format, eg: #include" bsp.h ";

Personal habits the non-standard reference library on the source file to see what the header file called

Third, typesetting program

Although the layout program does not affect the operation of the program (provided that your program is correct), but the chaotic program will make it difficult to understand the impact readability, for porting may be wasting a lot of time, even a long time I even read the program wrote it myself.

1, the appropriate blank line, increase line breaks and spaces:

First of all, we need to know a blank line with a newline without wasting memory, but it does not mean we can abuse, too many blank lines will only make the program seem too long, not good view of convergence, and the alignment of our common spaces with the code line breaks, then the question is, why sometimes we transplant program opens, there will be some confusion, it is because the different spaces with newline set of occupied spaces, specifically how to set up their own Baidu, the individual usually wrap symbol set occupies four spaces (since Attractive hhh), corresponding to a single space character is still space.

Personal preferences:

  • After each class declaration, function definition after the end of each blank line will be added;
  • Add a blank line between the function which is defined with the statements in order to distinguish the in vivo function defined region;
  • In some special statements function in vivo, increase a blank line, starting to pay attention;
  • "," To leave a space after, eg: Function (int x, int y, int z);
  • If the ";" symbol is not the end of a line, followed by a space to stay, eg: for (initialization; condition; update);
  • Assignment operators, comparison operators, arithmetic operators, logic operators, bit field operators, for
    example: "=", "+ =", "> =", "<=", "+", "*", "%" before and after "&&", "||", "<<", "^" operator should dihydric spaces;
  • "!" As a unary operator "-", "+", "-", "&" (address operator) before and after no space;

2, named (The following are personal habits, there are certain requirements)

Generally, we used uppercase and lowercase letters mixed with an underscore "_" split meaning of the word, so we want to express in clear as possible when naming an identifier to avoid confusion misunderstanding, but also a good identifier allows us to omit unnecessary comments .

  • Name the folder using the first letter in uppercase, lowercase file the full name;
  • Established source file somersault file must be the same file name correspondence;
  • Function name using the first letter of the word capitalized, abbreviations can be abbreviated as much as possible;
  • Parameter name using the first letter of its uppercase, lowercase behind;
  • Variable naming:
    ① static global variables, in front of the increase "s_" prefix;
    ② For global variables, in front of the increase "g_" prefix;
    ③ local variables are all lowercase;
    ④ static local variables, the first letter to uppercase ;
    ⑤ flag bit is defined by (51 core) or _Bool Boolean variable definition (required to support C99), and adds "_flag" suffix behind;
  • Macro definitions using all uppercase;
  • Debugging macro front was added _, eg: _DEBUG

3 comments

Requirements: simple, clear, clear, accurate, not repetition (ie repeat description), to ensure that the comment is consistent with the Code

  • Note the function block often used "/ * ... * /" and in front of the module, or allowed in the middle of the back;
  • Generally use the line comment "// ...";
  • Each should have front function interface description, mainly comprising: a function name, function, parameters, return values;
  • Each program should later plus / * - - END OF FILE - - * / shown in the end;
  • Global variables must be annotated;

Programming can be said to be an art, good programming habits will lead you further, also need over time, life is also true!

Finally, the article mainly refer to "high-quality programming guide C ++ / C language" ---- Lin Rui

Guess you like

Origin blog.csdn.net/qq_42992084/article/details/84504274