Free Programming Books
Free download ebooks on computer and programming

Free Java ebook "Bug Patterns in Java" Sample Chapter

Bug Patterns in Java
Free download chapter 2: Bugs, Specifications, and Implementations
Download chapter

Bug Patterns in Java presents a methodology for diagnosing and debugging computer programs. The act of debugging will be presented as an ideal application of the scientific method. Skill in this area is entirely independent of other programming skills, such as designing for extensibility and reuse. Nevertheless, it is seldom taught explicitly. Eric Allen lays out a theory of debugging, and how it relates to the rest of the development cycle. In particular, he stresses the critical role of unit testing in effective debugging. At the same time, he argues that testing and debugging, while often conflated, are properly considered to be distinct tasks.

Upon laying this groundwork, Allen then discusses various "bug patterns" (recurring relationships between signaled errors and underlying bugs in a program) that occur frequently in computer programs. For each pattern, the book discusses how to identify them, how to treat them, and how to prevent them.

< < prev next > >

Bugs, Specifications, and Implementations

WE RIGOROUSLY DEFINE THE concept of a bug, explain why a specification is crucial for controlling software bugs, highlight the differences between a specification and an implementation, and discuss a cost-effective means for developing specifications.

What Is a Bug?

This book is about debugging software. In order to discuss the act of debugging, it is important to define precisely what does and does not constitute a bug. For the purposes of this text, I will define a bug as "program behavior that deviates from its specification." This definition does not include:

  • Poor performance, unless a threshold level of performance is included as part of the specification.
  • An awkward or inefficient user interface. Although user interface design is an important topic, it's not the subject of this book.
  • Lack of features, lack of a particular useful feature, or lack of any feature not included in the program specification (even if it was intended to be in the specification).

The lack-of-features category illustrates an important aspect of our definition of bugs: they are inextricably linked to a program specification. If there is no program specification, then there literally are no bugs. To be sure, there are some generally accepted behavioral qualities expected from any software, e.g., it won't crash, it won't run forever without producing output, etc. Properties like these are implicitly part of the specification of any software. But these properties are the exception; most behavior must be explicitly specified. Because specifications define behavior which defines bugs, we had better discuss what constitutes a specification.

Intuitively, a program specification is a description of the behavior of a program. Therefore, having some kind of specification is essential to determining when the system is misbehaving. What form would we like this specification to take? First, let's consider how traditional software engineering answers this question.

Specification as Monolithic Treatise

The traditional method of software engineering is to develop a thorough specification of the system's functionality before entering a single line of code. This specification is made as formal as possible, so as to minimize ambiguities. The programmers then slog through the various details of this specification (often a large book) as they implement the system.

This method of specification was adapted from other engineering disciplines, where it can be extremely costly to make any changes to a specification after deployment begins. Microprocessor design is one of these disciplines. Currently, the specifications of microprocessors are interpreted and analyzed automatically. In fact, many aspects of a microprocessor design can be proven sound by unaided machines. But such techniques would be impossible if the specification weren't formalized.

In the software arena, where changes to a specification after deployment aren't nearly as costly, it's natural to question whether this style of up-front, formal specification is so useful. To consider this question, let's first examine how well that specification style works for a particular type of software artifact: a programming language.

Among software systems, programming languages are most similar to microprocessors in terms of the cost of modifying a specification. The cost of making even minor modifications to a language design after people have begun using it can be especially high. All the existing programs written in that language will have to be modified and recompiled.

As we might expect, the specifications of programming languages, compared with other software systems, are often quite formal, especially in the case of syntax. Virtually all modern programming languages have a formally specified syntax. Most parsers are constructed through the use of automatic parser-generators that read in such grammars and produce full-fledged parsers as output.