advanced computer science lab coding style & documentation
TRANSCRIPT
Advanced Computer Science Lab Coding Style
&Documentation
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 ThisVariableIsATemporaryCounter BUT 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 usersGOOD:"count_active_users()" BAD:"cntusr()" LOCAL variable names ->shortBAD: loop_counterGOOD: i or j
Functions
Short & SweetMax 1-2 screens of text (80x24)maximum length of a function is inversely proportional to the complexity and indentation level5-10 local vars or something’s WRONG=>rethink, split in smaller pieces
Commenting
GoodDanger: over commentingDON’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- SuggestionsAll 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()
Classes- Suggestions
You have made a mess…
Use “indent” with "-kr -i8" (stands for "K&R, 8 character indents") See “man indent” for moreIndent 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 tagsCommands: @class, @author, @date, @return, @param > doxygen –g doxyfile.cfg > doxygen doxyfile.cfg