This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Chapter 8 Correctness and Robustness I n previous chapters , we have covered the fundamentals of programming. The chapters that follow will cover more advanced aspects of programming. The ideas that are presented will be a little more complex and the programs that use them a little more complicated. This chapter is a kind of turning point in which we look at the problem of getting such complex programs right . Computer programs that fail are much too common. Programs are fragile. A tiny error can cause a program to misbehave or crash. Most of us are familiar with this from our own experience with computers. And we’ve all heard stories about software glitches that cause spacecraft to crash, telephone service to fail, and, in a few cases, people to die. Programs don’t have to be as bad as they are. It might well be impossible to guarantee that programs are problem-free, but careful programming and well-designed programming tools can help keep the problems to a minimum. This chapter will look at issues of correctness and robustness of programs. It also looks more closely at exceptions and the try..catch statement, and it introduces assertions , another of the tools that Java provides as an aid in writing correct programs. This chapter also includes sections on two topics that are only indirectly related to correct- ness and robustness. Section 8.5 will introduce threads while Section 8.6 looks briefly at the Analysis of Algorithms . Both of these topics do fit into this chapter in its role as a turning point, since they are part of the foundation for more advanced programming. 8.1 Introduction to Correctness and Robustness A program is correct if it accomplishes the task that it was designed to perform. It is robust if it can handle illegal inputs and other unexpected situations in a reasonable way. For example, consider a program that is designed to read some numbers from the user and then print the same numbers in sorted order. The program is correct if it works for any set of input numbers. It is robust if it can also deal with non-numeric input by, for example, printing an error message and ignoring the bad input. A non-robust program might crash or give nonsensical output in the same circumstance. Every program should be correct. (A sorting program that doesn’t sort correctly is pretty useless.) It’s not the case that every program needs to be completely robust. It depends on who will use it and how it will be used. For example, a small utility program that you write for your own use doesn’t have to be particularly robust. The question of correctness is actually more subtle than it might appear. A programmer 373 374 CHAPTER 8. CORRECTNESS AND ROBUSTNESS works from a specification of what the program is supposed to do. The programmer’s work is correct if the program meets its specification. But does that mean that the program itself is correct? What if the specification is incorrect or incomplete? A correct program should be a correct implementation of a complete and correct specification. The question is whether thecorrect implementation of a complete and correct specification....
View Full Document
This document was uploaded on 08/10/2011.
- Spring '11