What are the benefits to the programmer of using Abstract Data Types?

View video (15:55 minutes): .avi

What is an Abstract Data Type (ADT)?

An ADT is a programmer defined data type that is more abstract than the data types provided by the programming language.   ADT's provide a representation for data entities in the problem domain, e.g., Customer, Vehicle, Parts List, Address Book.  In Java, ADT's are implemented as a class. ADT's are fundamental to the Object Oriented Programming paradigm.

WITHOUT ADT's

Blackjack Program (source code)
 
// global data declarations
Card[] deckOfCards;
int currentPlayer;
int currentPosition;
Vector dealerHand;  
Vector userHand;

// functions
void runGame()
void playRound()
void shuffle()
Card deal()


Software can be written without ADT's.  Often this results in a large "global" data area and a long list of functions that manipulate this global data.  There are many disadvantages to this approach:
  • When reading the code, it may not be obvious that currentPosition and deckOfCards are dependent on each other. (Poor Readability)
  • The Blackjack game logic is intermingled with the deck manipulation functions.(Poor Readability)
  • Whenever the programmer manipulates the array, he or she must remember to update currentPosition. (Unwanted Complexity)
  • When trying to write a Blackjack game, in addition to keeping in mind the rules of Blackjack, the programmer also has to attend to the details of how the deck is implemented. (No Abstraction)
  • If you write another card game, you have to cut and paste source code for deck, and you have to be sure you get all the proper pieces. (Poor Reuse).
  • On team projects, another programmer might assume the deck is saved in the array from right-to-left instead of left-to-right and write code that changes how currentPosition is used, causing all the other functions to fail. (Poor Integrity)
  • If someone decides a Vector would be a better way to store the deck than an array, you have to carefully inspect the entire application to make sure every reference to the deck has been modified properly.  In addition, all the card games that use the copy-and-pasted code have to be changed. (Poor Maintainability)



WITH ADT's

Blackjack Logic
 
// data declarations
Deck deckOfCards;
int currentPlayer;


// functions
void runGame()
void playRound()

Deck
 
// data declarations
private Card[] deckOfCards;
private int currentPosition;


// operations
void shuffle()
Card deal()


An ADT encapsulates the data declarations with the operations that are performed on it.   Building software with ADT's results in a collection of interacting ADT's.

  • When reading the code for Deck, all the data members are relevant to Deck and nothing else. (Good Readability)
  • The Blackjack programmer can manipulate the Deck using high level operations like shuffle.(Simplicity)
  • When trying to write a Blackjack game, the programmer can focus on the rules of Blackjack. (Abstraction)
  • If you write another card game, you can simply "import" the Deck class. (Good Reuse).
  • On team projects, another programmer can't access the private fields of Deck. (High Integrity)
  • If someone decides a Vector would be a better way to store the deck than an array, it's easy to locate what needs to be changed. In addition, nothing needs to change in all the other card games that imported Deck.(Good Maintainability)
  • On team projects, the Deck and the logic can be developed simultaneously. (Productivity)
  • A well-tested Deck ADT is probably more reliable than writing it from scratch. (Reliability)



Summary of benefits of Abstract Data Types


Is it worth the bother of creating classes?  Why not just put all our code
in one giant main method?  Well, for really small programs, that's probably fine.
But as soon as you get over 100 lines or so, you'll start having difficulties.
ADT's solve many of these problems.

Simplicity

ADT's let us express our solutions using the vocabulary of the problem domain,
and the messy translation to the language level is hidden.  Thus our code is simpler
because it is at a higher level of abstraction, and inessential details are hidden.

Maintainability

All the operations and data are encapsulated in one place, so changes are isolated.
We can modify the implementation without altering the public interface thus
limiting the scope of changes.

Reusability

The ADT is a stand-alone component; all the data and operations are bundled together.
Thus it can be reused at the binary level (via import, for example), thereby reducing
the effort need to build upon it.

Integrity

Client programs can't directly modify private fields, thus preventing accidental
changes that could cause errors.