Convenții de cod - C: Diferență între versiuni

De la WikiLabs
(Example)
Linia 34: Linia 34:
 
  * Description: This file contains functions and variables used
 
  * Description: This file contains functions and variables used
 
  *        to exemplify the use of coding styles and how the
 
  *        to exemplify the use of coding styles and how the
  *        readability of a program improves when it is well written.
+
  *        readability of a program improves when it is well  
 +
*        written.
 
  ******************************************************************/
 
  ******************************************************************/
 +
 +
#include<stdio.h>
 +
#include<stdlib.h>
 +
 +
#define MAX_INT 0x7FFFFFFF
 +
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
 +
 +
typedef unsiged short Age
 +
typedef char* String
 +
 +
struct Person{
 +
    String first_name;
 +
    String last_name;
 +
    Age age;
 +
    String address;
 +
};
 +
 +
 
</syntaxhighlight>
 
</syntaxhighlight>

Versiunea de la data 9 martie 2014 11:01

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.
 ******************************************************************/

#include<stdio.h>
#include<stdlib.h>

#define MAX_INT 0x7FFFFFFF
#define MAX(a, b) ((a) >= (b) ? (a) : (b))

typedef unsiged short Age
typedef char* String

struct Person{
    String first_name;
    String last_name;
    Age age;
    String address;
};