Diferență între revizuiri ale paginii „Convenții de cod - C”
De la WikiLabs
Jump to navigationJump to search (Pagină nouă: During the DSA lab and while solving homework, you will also get points for the way the source code is paged, tabbed, the naming of the functions and variables, and the general struct...) |
|||
Linia 7: | Linia 7: | ||
** global variables (these you should avoid in most cases); | ** global variables (these you should avoid in most cases); | ||
** function prototypes; | ** function prototypes; | ||
− | ** function implementations; | + | ** function implementations; each implementation should have a short comment explaining the function role, the arguments and the return type; |
** footer comment, describing the changes in the file (this may be missing if the file is not under version control - svn, git, perforce, etc.); | ** footer comment, describing the changes in the file (this may be missing if the file is not under version control - svn, git, perforce, etc.); | ||
* all segments of the file are split by a comment-line which appears also between functions; | * all segments of the file are split by a comment-line which appears also between functions; | ||
Linia 26: | Linia 26: | ||
Example: | Example: | ||
+ | <syntaxhighlight lang="C"> | ||
+ | /******************************************************************* | ||
+ | * Coding Conventions Style Example | ||
+ | * | ||
+ | * Author: Radu Hobincu | ||
+ | * Date: 09.03.2014 | ||
+ | * Description: This file contains functions and variables used | ||
+ | * to exemplify the use of coding styles and how the | ||
+ | * readability of a program improves when it is well written. | ||
+ | ******************************************************************/ | ||
+ | </syntaxhighlight> |
Versiunea de la data 9 martie 2014 10:56
During the DSA lab and while solving homework, you will also get points for the way the source code is paged, tabbed, the naming of the functions and variables, and the general structure of the program. The conventions that need to be followed are:
- the segments of the file should appear in the following order:
- header comment, describing the role of the file, author and the project it is part of;
- import directives;
- define directives;
- data types (new structures or typedefs);
- global variables (these you should avoid in most cases);
- function prototypes;
- function implementations; each implementation should have a short comment explaining the function role, the arguments and the return type;
- footer comment, describing the changes in the file (this may be missing if the file is not under version control - svn, git, perforce, etc.);
- all segments of the file are split by a comment-line which appears also between functions;
- the main function, if it exists, is placed first;
- for if / do-while / for / case constructs and for method bodies, the open curly bracket is placed on the same line with the construct / function declaration and the closed curly bracket is placed on a separate line, on the same column with first letter from the construct/ declaration;
- an instruction block is indented by one extra tab in respect to the previous level;
- new data type names (as well as structures) start with an uppercase letter;
- function and variable names start with a lowercase letter;
- global variables start with a lowercase "g": (int g_my_global_var; or int gMyGlobalVar;)
- constant names are written in caps, words separated by the "_" character;
- the naming must be as explicit as possible regarding their purpose, in order to avoid excessive comments;
- it is allowed to insert a blank line, if a natural segregation of elements is obtained through it.
Notes:
- your code editor should be configured so that instead of a TAB character, it should insert 4 spaces;
- both camel case and underscore-separated words are accepted, as long as the notation is consistent throughout the program; so pick one and stick with it.
Example:
/*******************************************************************
* Coding Conventions Style Example
*
* Author: Radu Hobincu
* Date: 09.03.2014
* Description: This file contains functions and variables used
* to exemplify the use of coding styles and how the
* readability of a program improves when it is well written.
******************************************************************/