Docsity

Best Practices for Programming and Code Documentation, Study notes of Software Engineering

Partial preview of the text

Download Best Practices for Programming and Code Documentation and more Study notes Software Engineering in PDF only on Docsity!

How to design things well. How to control costs. So far, we've discussed the theory of software engineering. How do we best conduct ourselves while writing code? How can we best use our time? But in the end, it all comes down to writing programs. The practice of programming Tuesday, October 09, 2012 7:59 AM Docsity.com

Not necessarily the very best way to program. Simply an agreed-upon set of practices that lead to high-quality programs. No "theoretical" justification: this is a totally practical matter. In the following lectures, I present several aspects of programming usually called "best practices". "Best practices" Horror stories: do this "or you'll be sorry". Success stories: we did this and were happy with it. Two kinds of justification: "Best practices" Tuesday, October 09, 2012 8:01 AM Docsity.com

Hierarchical organization of code files. Consistency in indenting each kind of entity. Indentation: Document why , and not what. Document preconditions, postconditions, and invariants for every subroutine. Commenting: Conditional debugging: make debugging writes possible to turn on and off as needed. Antibugging: use code to explicitly check that expectations are fulfilled; stop error propogation. Portability: use conditional compilation to merge versions of programs intended for different architectures. Version control: utilize versions to keep track of changes. Coding: Some rather obvious best practices Some rather obvious best practices Tuesday, October 09, 2012 8:04 AM Docsity.com

Choose conventions for indentation. Be consistent within and across files. Indentation and readability My conventions for C and C++: int foo(arg1, arg2, …) { Contents indented ^ aligns } if (foo) { Contents indented ^ aligns } else { Contents indented ^ aligns } while (foo) { Contents indented ^ aligns } while (A ^ aligns Contents indented && B) { } Indentation and readability Tuesday, October 09, 2012 1:23 PM Docsity.com

The cohesive forces that cause it to be implemented in a single file. For every file, document: The theme of the section. For every section of a file, document: Purport: what it does. Arguments Preconditions Postconditions Test cases Examples For every function, document: Commenting best practices Commenting best practices Tuesday, October 09, 2012 4:05 PM Docsity.com

Literate programming: combining the documentation for the code with the code. (Knuth) Principle of locality: keep all documentation "near" the code it documents. Easiest to update documentation when code changes; discrepancies are easier to notice. Arguments for literate programming: Foremost tool for literate programming: doxygen Advanced commenting Advanced commenting Tuesday, October 09, 2012 9:26 AM Docsity.com

Too many options: choose one convention and be consistent. For the purposes of this exercise, /// /// This is doxygen text embedded in code. /// A blank /// line before. Embedded documentation. A blank /// line after. is the convention we will use. One other form proves handy: declaration; ///< documentation of declaration. Doxygen basics Doxygen basics Tuesday, October 09, 2012 9:31 AM Docsity.com

Start with
Inspired by LaTeX. Enable cross-referencing of code. Doxygen markups \file filename paragraph of description \brief Brief description of file. (no tag) Detailed description of file (optional/default keyword) \page pageName paragraphs of documentation for a "page", which is a unit of user documentation. For documenting files \param[in]: an input parameter. \param[out]: an output parameter. \param[in,out]: an input/output parameter. \param pname Parameter documentation \pre Paragraph of preconditions. \post Paragraph of postconditions. \return Paragraph describing any return values. \test Paragraph describing a test case. For documenting functions and procedures Doxygen markups Tuesday, October 09, 2012 12:59 PM Docsity.com

Be able to think in OO terms. With or without language support. Full power of doxygen Markups within a class: \extends className: is a subclass of. \implements classname: conforms to an interface. \class className Description of the class. \memberof className: declares a function as a member function in a language without native class support. \public, \private, \protected: same meanings as in C++. Markups within a member function Full power of doxygen Tuesday, October 09, 2012 1:20 PM Docsity.com

Allows construction of documentation pages by concatenation. Allows documenting non-code features such as user interfaces and help text. Doxygen page mode Doxygen page mode Tuesday, October 09, 2012 2:26 PM Docsity.com

See http://www.cs.tufts.edu/comp/250PSD/Practice/assert.h First: the file documentation: /// /// \file assert.h /// \brief Support for C and C++ assertion-based guard clauses and debugging. /// This file includes defines that report probable errors via \c STDERR /// including execution context (file and line number). Inline functions that /// do the actual printing are included here for speed. /// \version 1. /// A detailed example: Tuesday, October 09, 2012 2:36 PM Docsity.com

Documentation for a #define /// /// Test an expression and exit the program if it is false. /// \param[in] bool An expression to test, which should represent an /// integer value. /// \pre \c bool is a boolean expression that evaluates to an integer /// \post If \c bool evaluates to 0 (FALSE), error is printed to \c STDERR /// and the program terminates /// \test \c ASSERT(1) should print nothing. /// \test \c ASSERT(0) should print /// "Assertion 0 failed in line ??? of file ???" /// where \c ??? are the appropriate file name and line number, /// respectively. It should then terminate execution with exit code 1. /// #define ASSERT(bool) assert((bool),#bool,FILE,LINE) Documentation for a #define Tuesday, October 09, 2012 2:38 PM Docsity.com

All doxygen can do is static analysis. Static: determined by the shape of the source code. Doxygen doesn't help. Need other mechanisms. Dynamic: determined by the shape of execution. Static versus dynamic code analysis Tuesday, October 09, 2012 5:12 PM Docsity.com

So far, we've discussed how to document preconditions and postconditions for a function. It is also possible to make a function self- protecting, in the sense that it won't accept inputs not conforming to a specification. Key to this kind of protection: the assert.h I just documented. This is a dynamic analysis technique: determined by the shape of execution. Self-protecting code Next level: self-guarding code Tuesday, October 09, 2012 2:41 PM Docsity.com