Monday, May 16, 2011

Code Complete by Steve McConnell : Chapter 13

The author moves away from data to focusing on how to organize control flow in the code. The author provides following guidelines:

* Organize code so that dependencies are clear.
* Name routines so that dependencies are obvious.
* Use routine parameters to make dependencies clear.
* Document unclear dependencies.

The author advises to keep the code in such a way that it maintains the flow readable from top to bottom.

The author defines code between references of a variable as "window of vulnerability" and addition of new code in this area may inadvertently alter the variable. Because of this, the author suggest to keep references to a variable localized.
The author defines span as the number of lines of code between references of two variables. The authors advice the readers to compute the span of all the variables and get an average and organize the code in such a way so as to keep it as low as possible.
This enhances the readability of the code since the person can just focus on one section at a time.

The author then defines the concept of variable live time, the total number of statements over which a variable is live, i.e. from the statement where the variable is referenced first to the statement when it is referenced last. The author suggests to keep average value of this over all the variables low. This increases the readability and reduces the window of vulnerability.

The author also emphasizes the importance of putting related statements together or transfer them to create a newer smaller routine.

No comments:

Post a Comment