McConnell 2004 Chapter 17 Unusual Control Structures Flashcards
- What is McConnell’s overall opinion on ‘unusual’ control structures?
SEVERAL CONTROL CONSTRUCTS exist in a hazy twilight zone somewhere between being leading-edge and being discredited and disproved—often in both places at the same time! These constructs aren’t available in all languages but can be useful when used with care in those languages that do offer them.
- What is the programming language construct called which provides some means of exiting from a routine partway through the routine?
The return and exit statements are control constructs that enable a program to exit from a routine at will. They cause the routine to terminate through the normal exit channel, returning control to the calling routine. The word return is used here as a generic term for return in C++ and Java, Exit Sub and Exit Function in Visual Basic, and similar constructs.
- In what circumstances does McConnell recommend providing multiple exit points from a routine?
idk
With production-size code, the Exit Sub approach creates a noticeable amount of code before the nominal case is handled. The Exit Sub approach does avoid the
deep nesting of the first example, however, and, if the code in the first example were expanded to show setting an errorStatus variable, the Exit Sub approach would do a better job of keeping related statements together. When all the dust settles, the Exit Sub approach does appear more readable and maintainable, just not by a very wide margin. Minimize the number of returns in each routine It’s harder to understand a routine if, reading it at the bottom, you’re unaware of the possibility that it returned somewhere above. For that reason, use returns judiciously—only when they improve readability
- What general principle does McConnell recommend for exit points from a routine?(
Minimize the number of returns in each routine
It’s harder to understand a routine if, reading it at the bottom, you’re unaware of
the possibility that it returned somewhere above. For that reason, use returns
judiciously—only when they improve readability.
- What is a routine doing if it uses recursion?
a routine solves a small part of a problem itself, divides the problem into smaller pieces, and then calls itself to solve each of the smaller pieces.
- In most circumstances what alternative to recursion does McConnell recommend?
In general, recursion leads to small code and slow execution and chews up stack
space. For a small group of problems, recursion can produce simple, elegant
solutions. For a slightly larger group of problems, it can produce simple, elegant,
hard-to-understand solutions. For most problems, it produces massively
complicated solutions—in those cases, simple iteration is usually more
understandable. Use recursion selectively.
- What is a ‘goto’?
A well-placed goto can eliminate the need for duplicate code.
- What are strategies does McConnell recommend instead of using gotos?
Rewrite with nested if statements
485 To rewrite with nested if statements, nest the if statements so that each is
486 executed only if the previous test succeeds. This is the standard, textbook
487 programming approach to eliminating gotos.
- Complete the following statement of Steve McConnell and then explain what it means.
The field of software engineering has advanced largely through…”
restricting what programmers can do with their code.
Consequently, I view unconventional control structures with strong skepticism. I suspect that the majority of constructs in this chapter will eventually find their way onto the programmer’s scrap heap along with computed goto labels, variable routine entry points, self-modifying code, and other structures that favored flexibility and convenience over structure and ability to manage complexity.