12.8. Arrays

 < Free Open Study > 

Arrays are the simplest and most common type of structured data. In some languages, arrays are the only type of structured data. An array contains a group of items that are all of the same type and that are directly accessed through the use of an array index. Here are some tips on using arrays.

Make sure that all array indexes are within the bounds of the array In one way or another, all problems with arrays are caused by the fact that array elements can be accessed randomly. The most common problem arises when a program tries to access an array element that's out of bounds. In some languages, this produces an error; in others, it simply produces bizarre and unexpected results.

Consider using containers instead of arrays, or think of arrays as sequential structures Some of the brightest people in computer science have suggested that arrays never be accessed randomly, but only sequentially (Mills and Linger 1986). Their argument is that random accesses in arrays are similar to random gotos in a program: such accesses tend to be undisciplined, error prone, and hard to prove correct. They suggest using sets, stacks, and queues, whose elements are accessed sequentially, rather than using arrays.

In a small experiment, Mills and Linger found that designs created this way resulted in fewer variables and fewer variable references. The designs were relatively efficient and led to highly reliable software.


Consider using container classes that you can access sequentially sets, stacks, queues, and so on as alternatives before you automatically choose an array.

Check the end points of arrays Just as it's helpful to think through the end points in a loop structure, you can catch a lot of errors by checking the end points of arrays. Ask yourself whether the code correctly accesses the first element of the array or mistakenly accesses the element before or after the first element. What about the last element? Will the code make an off-by-one error? Finally, ask yourself whether the code correctly accesses the middle elements of the array.

Cross-Reference

Issues in using arrays and loops are similar and related. For details on loops, see Chapter 16, "Controlling Loops."


If an array is multidimensional, make sure its subscripts are used in the correct order It's easy to say Array[ i ][ j ] when you mean Array[ j ][ i ], so take the time to double-check that the indexes are in the right order. Consider using more meaningful names than i and j in cases in which their roles aren't immediately clear.

Watch out for index cross-talk If you're using nested loops, it's easy to write Array[ j ] when you mean Array[ i ]. Switching loop indexes is called "index cross-talk." Check for this problem. Better yet, use more meaningful index names than i and j to make it harder to commit cross-talk mistakes in the first place.

In C, use the ARRAY_LENGTH() macro to work with arrays You can build extra flexibility into your work with arrays by defining an ARRAY_LENGTH() macro that looks like this:

C Example of Defining an ARRAY_LENGTH() Macro
#define ARRAY_LENGTH( x ) (sizeof(x)/sizeof(x[0]))

When you use operations on an array, instead of using a named constant for the upper bound of the array size, use the ARRAY_LENGTH() macro. Here's an example:

C Example of Using the ARRAY_LENGTH() Macro for Array Operations
 ConsistencyRatios[] =    { 0.0, 0.0, 0.58, 0.90, 1.12,    1.24, 1.32, 1.41, 1.45, 1.49,    1.51, 1.48, 1.56, 1.57, 1.59 };    ... for ( ratioIdx = 0; ratioIdx < ARRAY_LENGTH( ConsistencyRatios ); ratioIdx++ );       <-- 1    ...

(1)Here's where the macro is used.

This technique is particularly useful for dimensionless arrays such as the one in this example. If you add or subtract entries, you don't have to remember to change a named constant that describes the array's size. Or course, the technique works with dimensioned arrays too, but if you use this approach, you don't always need to set up an extra named constant for the array definition.

 < Free Open Study > 


Code Complete
Code Complete: A Practical Handbook of Software Construction, Second Edition
ISBN: 0735619670
EAN: 2147483647
Year: 2003
Pages: 334

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net