8.3.1 Generators
A perceived risk of information hiding is that preventing client programs from
directly accessing critical data structures leads to an unacceptable loss of
efficiency. In the early days of data abstraction, many were concerned about the
cost of introducing extraneous function/method calls. Modern compilation
technology makes this concern moot. A more serious issue is that client
programs will be forced to use inefficient algorithms.
Consider the implementation of gradeReport in Figure 8.6. The invocation of
course.getStudents creates and returns a list of size n, where n is the number of
students. This is probably not a problem for a grade book for a single class, but
imagine keeping track of the grades of 1.7 million high school students taking
the SATs. Creating a new list of that size when the list already exists is a
significant inefficiency. One solution is to abandon the abstraction and allow
gradeReport to directly access the instance variable course.students, but that
would violate information hiding. Fortunately, there is a better solution.