advanced computer science lab coding style & documentation

15
Advanced Computer Science Lab Coding Style & Documentation

Upload: piers-chase

Post on 03-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Advanced Computer Science Lab Coding Style & Documentation

Advanced Computer Science Lab Coding Style

&Documentation

Page 2: 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.

Page 3: Advanced Computer Science Lab Coding Style & Documentation

Indentation

Easy to read Used to warn when functions are nested too deep

Page 4: Advanced Computer Science Lab Coding Style & Documentation

Placing Braces

preferred way ( Kernighan and Ritchie), is to put the opening brace last on the line, and put the closing brace first

Functions:

Page 5: Advanced Computer Science Lab Coding Style & Documentation

Placing Braces

Exceptions for closing bracket on its own line:

Page 6: Advanced Computer Science Lab Coding Style & Documentation

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

Page 7: Advanced Computer Science Lab Coding Style & Documentation

Naming

function that counts the number of active usersGOOD:"count_active_users()" BAD:"cntusr()" LOCAL variable names ->shortBAD: loop_counterGOOD: i or j

Page 8: Advanced Computer Science Lab Coding Style & Documentation

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

Page 9: Advanced Computer Science Lab Coding Style & Documentation

Commenting

GoodDanger: over commentingDON’T explain HOW code works BUT WHAT it does (&WHY)Not too many comments inside function, BUT at the head

Page 10: Advanced Computer Science Lab Coding Style & Documentation

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)

Page 11: Advanced Computer Science Lab Coding Style & Documentation

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()

Page 12: Advanced Computer Science Lab Coding Style & Documentation

Classes- Suggestions

Page 13: Advanced Computer Science Lab Coding Style & Documentation

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!

Page 14: Advanced Computer Science Lab Coding Style & Documentation

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

Page 15: Advanced Computer Science Lab Coding Style & Documentation

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