150 likes | 282 Vues
This guide outlines essential coding style and documentation practices for C programming. Emphasizing readability, it covers key areas such as indentation, brace placement, naming conventions, and commenting. It stresses the importance of using meaningful names for variables and functions, advocates for compact and clear functions, and advises against over-commenting. Suggestions for class and variable naming are provided, including prefix guidelines for visibility. Lastly, it introduces Doxygen as a tool for generating documentation from comments, ensuring code maintainability and understanding.
E N D
Indentation • Tabs are 8 characters, so indentations are also 8 characters. • if you need more than 3 levels of indentation you should fix your program.
Indentation • Easy to read • Used to warn when functions are nested too deep
Placing Braces • preferred way ( Kernighan and Ritchie), is to put the opening brace last on the line, and put the closing brace first • Functions:
Placing Braces • Exceptions for closing bracket on its own line:
Naming • C programmers do not use cute names like ThisVariableIsATemporaryCounterBUT rather “tmp” • GLOBAL variables (to be used only if you _really_ need them) need to have descriptive names, as do global functions
Naming • function that counts the number of active users • GOOD:"count_active_users()" • BAD:"cntusr()" • LOCAL variable names->short • BAD: loop_counter • GOOD: i or j
Functions • Short & Sweet • Max 1-2 screens of text (80x24) • maximum length of a function is inversely proportional to the complexity and indentation level • 5-10 local vars or something’s WRONG • =>rethink, split in smaller pieces
Commenting • Good • Danger: over commenting • DON’T explain HOW code works BUT WHAT it does (&WHY) • Not too many comments inside function, BUT at the head
Classes- Suggestions • public variables must begin with m like mFooVar(m=member) • protected variables must begin with mt, like mtFooVar and methods with t, like tFooNum(). (t=protected) • private variables must begin with mv, like mvFooVar and methods with v, like vFooLone().(v stands=private)
Classes- Suggestions • All public, protected and private variables must begin with uppercase after m like F in mFooVar. • All pointer variables must be prefixed with p, like • Public variables mpFooVar and methods like FooNum() • Protected variables mtpFooVar and methods with t like tFooNum() • Private variables mvpFooVar and methods with v like vFooNum()
You have made a mess… • Use “indent” with "-kr -i8" (stands for "K&R, 8 character indents") • See “man indent” for more • Indent does not fix bad programming!
Documentation Style • Standard C++ Comments • -Allow maintenance of code by people not involved in its development. • -Allow understanding of requirements, inputs and outputs, algorithms, and software design techniques, when reviewing code. • -Allow understanding of code structure for purposes of reusability
Doxygen • mainly extracted from the comments in header files • description of a class is placed before the class statement (/** .... */ or ///…) • Can embed HTML tags • Commands: @class, @author, @date, @return, @param • > doxygen –g doxyfile.cfg • > doxygen doxyfile.cfg