Date Published: June 29, 2020
Code Complete by Steve McConnell is a well-written explanation of themes in software construction.
I'm working my way through the second edition of Code Complete by Steve McConnell. Here are my notes from Part 4: Statements.
Part 4 includes chapters 14-19: Organizing Straight-Line Code, Using Conditionals, Controlling Loops, Unusual Control Structures, Table-Driven Methods, and General Control Issues.
Dependencies mean that one line of code depends on another having already been executed. Therefore, dependencies lead to sequences being executed in a set order. When building code with dependencies, make them very clear.
When working with statements whose order doesn't matter:
don't make up phony variables to be able to use the case statement
breakto avoid dropping through the end of a case statement
whiletests condition at beginning,
do-whiletests condition at end
goto, or other statement
while(true), for example)
whileloops should be used in more complex applications
while(true)for infinite loops
forloops when appropriate
forloop if a
whileloop is more appropriate
Processing the middle of a loop
Exiting a loop
forloop to make the loop terminate
whileloop instead if the above seems necessary
breakto exit the loop and resume program at first line following the loop
continueto skip to the next iteration of a loop
breakstatements instead of boolean flags in a
breakstatements strewn through it
continue for tests at the top of a loop
continuetowards the middle or end of a loop, use an
breakstructure if your language supports it
continueonly with caution
Using loop variables
How long should a loop be?
To create a complex loop, it's sometimes best to work from the inside-out:
Multiple returns from a routine
returnstatement in C++ or Java allows a routine to terminate partway through
returnto enhance readability
gotos should be used only when other options have been carefully considered
error processing involves testing for errors every step of the way and then deallocating resources at the end.
gotos can clean this up nicely, but alternatives include:
gotowhen they have two conditional tests and an else clause and they want to execute code in one condition and the else clause
gotowould have pointed to
gotowith a simple, more readable solution
Issues that need addressed when using table-driven methods:
Fudging table-lookup keys - for when data isn't well-behaved
Indexes use primary data to look up a key in an index table, then use that value to look up the data you're interested in
Stair-step access tables are less direct than indexes, but they waste less space
while( (a>b) ) = true)
ifstatements, be sure that there is a positive condition for the
apply DeMorgan's Theorems to simplify boolean tests with negatives:
Compound statements (aka Blocks)
use braces to clarify conditionals (
DoNothing()to make it super clear nothing is supposed to be done
breakblock to simplify - only use if your whole team is familiar with this approach, as it's fairly rare
ifstatements with if-then-else statements, which greatly increases readability
To measure complexity, you can count the number of decision points in your routine - 1 for the direct path, then another decision point for each of the following: if, while, repeat, for, and, or
Thanks for reading! I hope you find this and other articles here at ilyanaDev helpful! Be sure to follow me on Twitter @ilyanaDev.