Convenții de cod - C

De la WikiLabs
Versiunea din 9 martie 2014 10:51, autor: Rhobincu (discuție | contribuții) (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...)
(dif) ← Versiunea anterioară | Versiunea curentă (dif) | Versiunea următoare → (dif)
Jump to navigationJump to search

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;
    • 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: