Coding Paradigms

The studies presented in this chapter deal with distinctive aspects of Ada arising in the coding of statements and expressions. The study “Use of Slices” describes how loops processing arrays one element at a time can often be rewritten in Ada as single a

  • PDF / 2,578,959 Bytes
  • 30 Pages / 439.37 x 666.14 pts Page_size
  • 50 Downloads / 197 Views

DOWNLOAD

REPORT


Coding Paradigms

The studies presented in this chapter deal with distinctive aspects of Ada arising in the coding of statements and expressions . The study "Use of Slices" describes how loops processing arrays one element at a time can often be rewritten in Ada as single assignment statements processing an entire slice. The second study discusses short circuit control forms, explaining the intended use of the and then and or else operations: not to optimize the evaluation of Boolean expressions, but to allow Boolean expressions in which one operand might not be defined in cases where the value of that operand cannot affect the ultimate result anyway. One of the examples illustrating the proper use of short circuit control forms, a loop to search an array for a particular value, also figures prominently in case study 3.3, "Loop Statements," which compares various alternatives for writing loops and specifying how loops terminate. The final study, "Use of Block Statements for Local Renaming," shows how a block statement containing renaming declarations can be used in much the same way as a Pascal with statement.

3.1 Use of Slices To many programmers, slices will appear as a novel feature of Ada, a feature they are not sure how to use. The following case study explores the problem of determining programming situations in which the use of slices promotes readability and ease of maintenance. C. N. Ausnit et al., Ada® in Practice © Springer Japan 1985

65

3.1 Use of Slices

Slicesare a useful way of identifying a part of a one-dimensional array. They facilitate the assignment and comparison of parts of arrays because they obviate the need to do parts of these assignments componentwise, through for loops. This case study will present two different applications involving slices, the first an alternative to a for loop and the second an instance where a subtype identifies the slice.

Problem A: Operations on Message Strings PROBLEM STATEMENT

The example discussed here is abstracted from our message-switching system. The basic unit of data being processed is a message, which is ultimately a sequence of characters, an entity that can be represented as a text string. Various operations can be done on this string or a substring of it, including writing to the string, reading from the string, and comparing it to another string. In many language s, these operations are implemented using a for loop, as illustrated in Figure 3-1. Two of these examples show operations on a substring of a string because Ada allows the assignment and comparison of entire arrays. (The code excerpts in Figure 3-1 make certain assumptions that might not necessarily apply in another situation. For example, they assume that the source and target strings have the same absolute indices. In the event that they only had the same relative indices, then a new index, say J, would have to be introduced and specifically incremented inside the loop, or an offset would have to be added to one of the subscripts.) (i)

for I in Lower Bound .. Upper Bound loop Target