17 - 20:22:25 17 CS 61B Lecture 17 Wednesday October 6 2010...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
10/01/10 20:22:25 1 17 CS 61B: Lecture 17 Wednesday, October 6, 2010 Today’s reading: ENCAPSULATION ============= A _module_ is a set of methods that work together as a whole to perform some task or set of related tasks. A module is _encapsulated_ if its implementation is completely hidden, and it can be accessed only through a documented interface. As you know, an abstract data type (ADT) is an encapsulated data structure. Not all encapsulated modules are ADTs, though. Algorithms (like list sorters) and applications (like network routing software) can also be encapsulated, even if they are distinct from the data structures they use. So far, I’ve discussed encapsulation as a way of preventing "evil tamperers" from corrupting your data structures. Who are these evil tamperers? Sometimes, they’re your coworkers, or other programmers who will work on a project long after you’re gone. Often the evil tamperer is you. A Cautionary Tale ----------------- Doug Whole, a programmer at a Silicon Valley startup, implements a singly- linked list much like the one you used in Homework 3, but all its fields are public. Doug also writes application code that uses linked lists. One day, Doug needs to write code that splices the second node out of a list. It would only take one line, and he doesn’t foresee ever needing to use the same operation anywhere else. Being lazy, Doug doesn’t feel like adding a new method to the List class. Instead, he just does the work directly. public class ListMangler { [lotsa code] /* Gosh, I am sooooooooooooooooooo tired. */ list.head.next = list.head.next.next; [lotsa more code] } Two years later, another programmer, Jeannie Yess, decides to improve the speed of their list data structure. After careful thought, she decides to reprogram the List class so that it uses doubly-linked lists internally. A "previous" field is added to ListNode, and the List methods are rewritten. Jeannie tests her new List implementation extensively, and can find no bugs. But when she replaces Doug’s List class with her own, the company’s landmark ListMangler application repeatedly produces the wrong results. After a long day of debugging, Jeannie discovers the culprit: Doug’s single line of code. This kind of bug is one of the most difficult to find and fix. It’s also very common in commercial software systems, and it can have far-reaching effects. You see, Doug’s line of code is not the only one that reads or modifies the list data structure directly. Jeannie still has to debug 100,000 lines of Doug’s code in other failing applications, as well as 500,000 lines more written by other programmers who also directly manipulated ListNodes. The List improvement project is abandoned. A Remedy:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/10/2012 for the course CS 61B taught by Professor Canny during the Fall '01 term at Berkeley.

Page1 / 2

17 - 20:22:25 17 CS 61B Lecture 17 Wednesday October 6 2010...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online