McConnell 2004 Chapter 19 General Control Issues Flashcards
- What is wrong with using the values 0 and 1 as flags rather than using Boolean expressions?
It’s not clear from reading the code whether the function calls are executed when the tests are true or when they’re false. Nothing in the code fragment itself tells you whether 1 represents true and 0 false or whether the opposite is true. It’s not even clear that the values 1 and 0 are being used to represent true and false. For example, in the If reportSelected = 1 line, the 1 could easily represent the first report, a 2 the second, a 3 the third; nothing in the code tells you that 1
represents either true or false. It’s also easy to write 0 when you mean 1 and vice
versa.
- What are two reasons why tests written with implicit comparisons are clearer than explicit comparisons? (p433) ie while ( a > b ) do is clearer than while ( ( a> b ) = true ) do
????
- What are three ways to simplify complicated Boolean expressions?
- Break complicated tests into partial tests with new boolean variables.
- Move complicated expressions into boolean functions
- Use decision tables to replace complicated conditions.
- What is DeMorgan’s Theorem and how can it be used to simplify a Boolean test with negatives
DeMorgan’s Theorems let you exploit the logical relationship between an expression and a version of the expression that means the same thing because it’s doubly negated. For example, you might have a code fragment that contains the following test:
To apply DeMorgan’s Theorems to the logical operator and or the logical operator or and a pair of operands, you negate each of the operands, switch the ands and ors, and negate the entire expression.
What alternative should be used rather than relying on a programmer’s understanding of a particular programming language’s expression evaluation order?
If you have a complicated boolean expression, rather than relying on the language’s evaluation order, parenthesize to make your meaning clear. Using parentheses makes less of a demand on your reader, who might not understand the subtleties of how your language evaluates boolean expressions. If you’re smart, you won’t depend on your own or your reader’s in-depth memorization of evaluation precedence—especially when you have to switch among two or more languages. Using parentheses isn’t like sending a telegram: you’re not charged for each character—the extra characters are free.
- What order does McConnell recommend for organizing numeric expressions? Why does he make this recommendation?
Organize numeric tests so that they follow the points on a number line. In general, structure your numeric tests so that you have comparisons like
MIN_ELEMENTS < MIN_ELEMENTS or MAX_ELEMENTS < i
The idea is to order the elements left to right, from smallest to largest. In the first line, MIN_ELEMENTS and MAX_ELEMENTS are the two endpoints, so they go at the ends. The variable i is supposed to be between them, so it goes in the middle. In the second example, you’re testing whether i is outside the range, so i goes on the outside of the test at either end and MIN_ELEMENTS and MAX_ELEMENTS go on the inside. This approach maps easily to a visual image
of the comparison:
- What is McConnell’s maximum recommended level for nesting if statements?
Many researchers recommend avoiding nesting to more than three or four levels.
- What are three techniques which can be used to avoid excessive nested if statements?
- Simplify a nested if by retesting part of the condition.
- Simplify a nested if by using a break block.
- Convert a nested if to a set of if-then-elses
- Convert a nested if to a case statement
- Factor deeply nested code into its own routine
- Use a more object-oriented approach
- Redesign deeply nested code
- What is the core concept of “Structured Programing” and why is it recommended?
The core of structured programming is the simple idea that a program should use only one-in, one-out control constructs (also called single-entry, single-exit control constructs). A one-in, one-out control construct is a block of code that has only one place it can start and only one place it can end. It has no other entries or exits. Structured programming isn’t the same as structured, top-down design. It applies only at the detailed coding level A structured program progresses in an orderly, disciplined way, rather than jumping around unpredictably. You can read it from top to bottom, and it executes in much the same way. Less disciplined approaches result in source code that provides a less meaningful, less readable picture of how a program executes in the machine. Less readability means less understanding and, ultimately, lower program quality. The central concepts of structured programming are still useful today and apply to considerations in using break, continue, throw, catch, return, and other topics.
- How you can measure the complexity of a routine?
complexity is measured by counting the number of “decision points” in a routine.
- Start with 1 for the straight path through the routine.
- Add 1 for each of the following keywords, or their equivalents: if while
repeat for and or
Here’s an example:
if ( ( (status = Success) and done ) or
( not done and ( numLines >= maxLines ) ) ) then …
In this fragment, you count 1 to start; 2 for the if; 3 for the and; 4 for the or; and
5 for the and. Thus, this fragment contains a total of five decision points.