Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

17.8 Assertions 253 as in fly-by-wire airplanes, each version runs on a separate (but identical) processor. The voting module is small and simple, consuming minimal developer and processor time. For obvious reasons, an even number of versions is not appropriate. The main difference between the recovery block and the n-version schemes is that in the former the different versions are executed sequentially (if need be). Is n-programming forward error recovery or is it backward error recovery? The answer is that, once an error is revealed, the correct behavior is immediately available and the system can continue forwards. So it is forward error recovery. 17.8 ● Assertions Assertions are statements written into software that say what should be true of the data. Assertions have been used since the early days of programming as an aid to verifying the correctness of software. An assertion states what should always be true at a particular point in a program. Assertions are usually placed: ■ at the entry to a method – called a precondition, it states what the relationship between the parameters should be ■ at the end of a method – called a postcondition, it states what the relationship between the parameters should be ■ within a loop – called a loop invariant, it states what is always true, before and after each loop iteration, however many iterations the loop has performed. ■ at the head of a class – called a class invariant, it states what is always true before and after a call on any of the class’s public methods. The assertion states a relationship between the variables of an instance of the class. An example should help see how assertions can be used. Take the example of a class that implements a data structure called a stack. Items can be placed in the data structure by calling the public method push and removed by calling pop. Let us assume that the stack has a fixed length, described by a variable called capacity. Suppose the class uses a variable called count to record how many items are currently in the stack. Then we can make the following assertions at the level of the class. These class invariant is: assert count >= 0; assert capacity >= count; These are statements which must always be true for the entire class, before or after any use is made of the class. We can also make assertions for the individual methods. Thus for method push, we can say as a postcondition: assert newCount = oldCount + 1; For the method push, we can also state the following precondition: assert oldCount < capacity;

254 Chapter 17 ■ Software robustness SELF-TEST QUESTION 17.9 Write pre- and post-conditions for method pop. Note that truth of assertions does not guarantee that the software is working correctly. However, if the value of an assertion is false, then there certainly is a fault in the software. Note also that violation of a precondition means that there is a fault in the user of the method; a violation of a postcondition means a fault in the method itself. There are two main ways to make use of assertions. One way is to write assertions as comments in a program, to assist in manual verification. On the other hand, as indicated by the notation used above, some programming languages (including Java) allow assertions to be written as part of the language – and their correctness is checked at runtime. If an assertion is found to be false, an exception is thrown. There is something of an argument about whether assertions should be used only during development, or whether they should also be enabled when the software is put into productive use. 17.9 ● Discussion Fault tolerance in hardware has long been recognized – and accommodated. Electronic engineers have frequently incorporated redundancy, such as triple modular redundancy, within the design of circuits to provide for hardware failure. Fault tolerance in software has become more widely addressed in the design of computer systems as it has become recognized that it is almost impossible to produce correct software. Exception handling is now supported by all the mainstream software engineering languages – Ada, C++, Visual Basic, C# and Java. This means that designers can provide for failure in an organized manner, rather than in an ad hoc fashion. Particularly in safety-critical systems, either recovery blocks or n-programming is used to cope with design faults and enhance reliability. Fault tolerance does, of course, cost money. It requires extra design and programming effort, extra memory and extra processing time to check for and handle exceptions. Some applications need greater attention to fault tolerance than others, and safety-critical systems are more likely to merit the extra attention of fault tolerance. However, even software packages that have no safety requirements often need fault tolerance of some kind. For example, we now expect a word processor to perform periodic and automatic saving of the current document, so that recovery can be performed in the event of power failure or software crash. End users are increasingly demanding that the software cleans up properly after failures, rather than leave them with a mess that they cannot salvage. Thus it is likely that ever-increasing attention will be paid to improving the fault tolerance of software.

17.8 Assertions 253<br />

as in fly-by-wire airplanes, each version runs on a separate (but identical) processor. The<br />

voting module is small and simple, consuming minimal developer and processor time.<br />

For obvious reasons, an even number of versions is not appropriate.<br />

The main difference between the recovery block and the n-version schemes is that<br />

in the <strong>for</strong>mer the different versions are executed sequentially (if need be).<br />

Is n-programming <strong>for</strong>ward error recovery or is it backward error recovery? The<br />

answer is that, once an error is revealed, the correct behavior is immediately available<br />

and the system can continue <strong>for</strong>wards. So it is <strong>for</strong>ward error recovery.<br />

17.8 ● Assertions<br />

Assertions are statements written into software that say what should be true of the data.<br />

Assertions have been used since the early days of programming as an aid to verifying the<br />

correctness of software. An assertion states what should always be true at a particular<br />

point in a program. Assertions are usually placed:<br />

■ at the entry to a method – called a precondition, it states what the relationship<br />

between the parameters should be<br />

■ at the end of a method – called a postcondition, it states what the relationship<br />

between the parameters should be<br />

■ within a loop – called a loop invariant, it states what is always true, be<strong>for</strong>e and after<br />

each loop iteration, however many iterations the loop has per<strong>for</strong>med.<br />

■ at the head of a class – called a class invariant, it states what is always true be<strong>for</strong>e<br />

and after a call on any of the class’s public methods. The assertion states a relationship<br />

between the variables of an instance of the class.<br />

An example should help see how assertions can be used. Take the example of a class<br />

that implements a data structure called a stack. Items can be placed in the data structure<br />

by calling the public method push and removed by calling pop. Let us assume that<br />

the stack has a fixed length, described by a variable called capacity. Suppose the class<br />

uses a variable called count to record how many items are currently in the stack. Then<br />

we can make the following assertions at the level of the class. These class invariant is:<br />

assert count >= 0;<br />

assert capacity >= count;<br />

These are statements which must always be true <strong>for</strong> the entire class, be<strong>for</strong>e or after<br />

any use is made of the class. We can also make assertions <strong>for</strong> the individual methods.<br />

Thus <strong>for</strong> method push, we can say as a postcondition:<br />

assert newCount = oldCount + 1;<br />

For the method push, we can also state the following precondition:<br />

assert oldCount < capacity;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!