13.1. Structures

 < Free Open Study > 

The term "structure" refers to data that's built up from other types. Because arrays are a special case, they are treated separately in Chapter 12. This section deals with user-created structured data structs in C and C++ and Structures in Microsoft Visual Basic. In Java and C++, classes also sometimes perform as structures (when the class consists entirely of public data members with no public routines).

You'll generally want to create classes rather than structures so that you can take advantage of the privacy and functionality offered by classes in addition to the public data supported by structures. But sometimes directly manipulating blocks of data can be useful, so here are some reasons for using structures:

Use structures to clarify data relationships Structures bundle groups of related items together. Sometimes the hardest part of figuring out a program is figuring out which data goes with which other data. It's like going to a small town and asking who's related to whom. You come to find out that everybody's kind of related to everybody else, but not really, and you never get a good answer.

If the data has been carefully structured, figuring out what goes with what is much easier. Here's an example of data that hasn't been structured:

Visual Basic Example of Misleading, Unstructured Variables
name = inputName address = inputAddress phone = inputPhone title = inputTitle department = inputDepartment bonus = inputBonus

Because this data is unstructured, it looks as if all the assignment statements belong together. Actually, name, address, and phone are variables associated with individual employees, and title, department, and bonus are variables associated with a supervisor. The code fragment provides no hint that there are two kinds of data at work. In the code fragment below, the use of structures makes the relationships clearer:

Visual Basic Example of More Informative, Structured Variables
employee.name = inputName employee.address = inputAddress employee.phone = inputPhone supervisor.title = inputTitle supervisor.department = inputDepartment supervisor.bonus = inputBonus

In the code that uses structured variables, it's clear that some of the data is associated with an employee, other data with a supervisor.

Use structures to simplify operations on blocks of data You can combine related elements into a structure and perform operations on the structure. It's easier to operate on the structure than to perform the same operation on each of the elements. It's also more reliable, and it takes fewer lines of code.

Suppose you have a group of data items that belong together for instance, data about an employee in a personnel database. If the data isn't combined into a structure, merely copying the group of data can involve a lot of statements. Here's an example in Visual Basic:

Visual Basic Example of Copying a Group of Data Items Clumsily
newName = oldName newAddress = oldAddress newPhone = oldPhone newSsn = oldSsn newGender = oldGender newSalary = oldSalary

Every time you want to transfer information about an employee, you have to have this whole group of statements. If you ever add a new piece of employee information for example, numWithholdings you have to find every place at which you have a block of assignments and add an assignment for newNumWithholdings = oldNumWithholdings.

Imagine how horrible swapping data between two employees would be. You don't have to use your imagination here it is:

Visual Basic Example of Swapping Two Groups of Data the Hard Way

' swap new and old employee data previousOldName = oldName previousOldAddress = oldAddress previousOldPhone = oldPhone previousOldSsn = oldSsn previousOldGender = oldGender previousOldSalary = oldSalary oldName = newName oldAddress = newAddress oldPhone = newPhone oldSsn = newSsn oldGender = newGender oldSalary = newSalary newName = previousOldName newAddress = previousOldAddress newPhone = previousOldPhone newSsn = previousOldSsn newGender = previousOldGender newSalary = previousOldSalary


An easier way to approach the problem is to declare a structured variable:

Visual Basic Example of Declaring Structures
Structure Employee    name As String    address As String    phone As String    ssn As String    gender As String    salary As long End Structure Dim newEmployee As Employee Dim oldEmployee As Employee Dim previousOldEmployee As Employee

Now you can switch all the elements in the old and new employee structures with three statements:

Visual Basic Example of an Easier Way to Swap Two Groups of Data
previousOldEmployee = oldEmployee oldEmployee = newEmployee newEmployee = previousOldEmployee

If you want to add a field such as numWithholdings, you simply add it to the Structure declaration. Neither the three statements above nor any similar statements throughout the program need to be modified. C++ and other languages have similar capabilities.

Use structures to simplify parameter lists You can simplify routine parameter lists by using structured variables. The technique is similar to the one just shown. Rather than passing each of the elements needed individually, you can group related elements into a structure and pass the whole enchilada as a group structure. Here's an example of the hard way to pass a group of related parameters:

Visual Basic Example of a Clumsy Routine Call Without a Structure
HardWayRoutine( name, address, phone, ssn, gender, salary )

Cross-Reference

For details on how much data to share between routines, see "Keep Coupling Loose" in Section 5.3.


And this is an example of the easy way to call a routine by using a structured variable that contains the elements of the first parameter list:

Visual Basic Example of an Elegant Routine Call with a Structure
EasyWayRoutine( employee )

If you want to add numWithholdings to the first kind of call, you have to wade through your code and change every call to HardWayRoutine(). If you add a numWithholdings element to Employee, you don't have to change the parameters to EasyWayRoutine() at all.

You can carry this technique to extremes, putting all the variables in your program into one big, juicy variable and then passing it everywhere. Careful programmers avoid bundling data any more than is logically necessary. Furthermore, careful programmers avoid passing a structure as a parameter when only one or two fields from the structure are needed they pass the specific fields needed instead. This is an aspect of information hiding: some information is hidden in routines, and some is hidden from routines. Information is passed around on a need-to-know basis.

Cross-Reference

For details on the hazards of passing too much data, see "Keep Coupling Loose" in Section 5.3.


Use structures to reduce maintenance Because you group related data when you use structures, changing a structure requires fewer changes throughout a program. This is especially true in sections of code that aren't logically related to the change in the structure. Since changes tend to produce errors, fewer changes mean fewer errors. If your Employee structure has a title field and you decide to delete it, you don't need to change any of the parameter lists or assignment statements that use the whole structure. Of course, you have to change any code that deals specifically with employee titles, but that is conceptually related to deleting the title field and is hard to overlook.

The big advantage of structured the data is found in sections of code that bear no logical relation to the title field. Sometimes programs have statements that refer conceptually to a collection of data rather than to individual components. In such cases, individual components, such as the title field, are referenced merely because they are part of the collection. Such sections of code don't have any logical reason to work with the title field specifically, and those sections are easy to overlook when you change title. If you use a structure, it's all right to overlook such sections because the code refers to the collection of related data rather than to each component individually.

 < 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