Programmers have been using an approach they call “object-oriented programming” since the 1980s. That crowd had always been unable to accept that what they were actually doing was just “class-oriented programming,” or abstract data types. Object-oriented programming is a whole different story, and few design methods or programming languages ever evolved to support it. Winding back history’s clock to Alan Kay’s original notion of what objects were envisioned to be, Jim Coplien and Trygve Reenskaug have laid new foundations for real object-oriented programming. It’s called DCI: Data, Context, and Interaction. We see these principles exemplified in the trygve programming language.
This talk will overview why real object-oriented programming is important, will illustrate the principles of the DCI paradigm with concrete code, and will present formal research results showing the superiority of the DCI approach.
Valuable contributions to this topic have been made by:
James O. Coplien, Gertrud & Cope
Héctor Adrián Valdecantos, RIT
Mehdi Mirakhorli, RIT
"An introduction to object-oriented programming for those who have never done it before… which probably includes you ", James Coplien
1. An introduction to object-oriented
programming for those who have
never done it before… which
probably includes you
>
>
Gertrud & Cope
James Coplien
RIT
Héctor Adrián Valdecantos,
Mehdi Mirakhorli
2. A COMPARISON
Abstract DataTypes
Object-Oriented
Programming
DCI
State and Behavior No data
OOP in Contexts, data in
classes (no data in roles)
Objects as loci of behavior
Networks of objects with
aggregate behavior
Networks of objects
(Contexts) with aggregate
behavior
Compile-time binding Run-time binding
Use case relationships
compile-time bound; instance
methods run-time bound
3. WETHINK IN,AND DELIVER,
USE CASES
• We think in, and deliver, use cases
• You can’t find a use case in Java code
4. ABSTRACTTYPES ARE ABOUTTHE
BEHAVIOR OF INDIVIDUAL OBJECTS
• “Abstract” in the sense that the implementation is
encapsulated
• Inheritance as an implementation technique
5. TWO PROBLEMS
• The first problem: We write classes — not objects
• We think of one class (data locale) at a time
• We can’t easily reason about the code structure of a use case
• This causes, at best, context switching
• The second problem: we focus on one object at a time
• TDD, unit testing
• Polymorphism means that we can focus on the methods of only one class at a time — the
binding to methods for other objects is deferred!
6. OBJECT-ORIENTED PROGRAMMIMG
IS ABOUT USE CASES
• A network of cooperating objects solving a problem
• “If we look at the whole history, we see that the proto-OOP
stuff started with ADT, had a little fork towards what I called
“objects” — that led to Smalltalk, etc., — but after the little
fork, the CS establishment pretty much did ADT and wanted
to stick with the data-procedure paradigm.” — Alan Kay
10. context TransferMoneyContext {
role DESTINATION_ACCOUNT {
public void transferFrom() {
this.increaseBalance(AMOUNT);
this.updateLog("Transfer in", new Date(), AMOUNT);
}
public void increaseBalance(Currency amount);
public void updateLog(String msg, Date time, Currency amount)
} requires {
void increaseBalance(Currency amount);
void updateLog(String msg, Date time, Currency amount)
}
public TransferMoneyContext(Currency amount, Account source, Account destination)
{
SOURCE_ACCOUNT = source;
DESTINATION_ACCOUNT = destination;
AMOUNT = amount
}
. . . .
public void doit()
{
SOURCE_ACCOUNT.transferTo()
}
11. role SOURCE_ACCOUNT {
public void transferTo() {
/
/ This code is reviewable and meaningfully testable with stubs!
int SUCCESS_DEPOSIT_SCREEN = 10;
beginTransaction();
if (this.availableBalance() < AMOUNT) {
endTransaction();
assert(false, "Unavailable balance")
} else {
this.decreaseBalance(AMOUNT);
DESTINATION_ACCOUNT.increaseBalance(AMOUNT);
this.updateLog("Transfer Out", new Date(), AMOUNT);
DESTINATION_ACCOUNT.updateLog("Transfer In", new Date(), AMOUNT);
}
GUI.displayScreen(SUCCESS_DEPOSIT_SCREEN);
endTransaction()
}
} requires {
void decreaseBalance(Currency amount);
Currency availableBalance() const;
void updateLog(String msg, Date time, Currency amount)
}
12. class CheckingAccount extends Account {
public CheckingAccount() {
availableBalance_ = new Euro(100.00)
}
public Currency availableBalance() const
{
return availableBalance_
}
public void decreaseBalance(Currency c) {
availableBalance_ = availableBalance_ - c
}
public void updateLog(String message, Date t, Currency c) const {
System.out.print("account: ").print(accountID())
.print(" CheckingAccount::updateLog("").print(message)
.print("", ").print(t.toString()).print(", ")
.print(c.toString()).print(“)")
.println()
}
public void increaseBalance(Currency c) {
availableBalance_ = availableBalance_ + c
}
private Currency availableBalance_
}
13. DATA, CONTEXT AND
INTERACTION
• We still have classes, with their data, that reflect the all-
important domain model
• The Context represents the network of connections for a use
case of any set of interacting objects
• The Roles specify the concrete interactions between objects
along this network, and the interactions with the data
14. RESEARCH RESULTS
• The trygve programming language: a pure object-oriented
language
• Compare with Java — an abstract type language
• Results indicate that DCI code in trygve supports higher
comprehension regarding correctness, and that it improves the
locality of reference, reducing context switching during software
discovery
16. CONCLUSION
• Much of the original object vision (networks of objects, late
binding, end-user mental models) has been lost
• DCI restores the ability to express the original vision
• DCI is available in a broad spectrum of programming
technologies
• Empirical results are encouraging, and research continues