Beruflich Dokumente
Kultur Dokumente
University of Glasgow
Overview
API design & enforcement
The mistakes programmers make
What is typestate?
What is Hanoi?
Future work
API Specification
Designer must be as precise as possible
What will be provided by an implementation
What will be expected in return from the client
... a contract!
Implementation and client code must each hold up their
end of the bargain
What happens when a contract is violated?
The API Police!
Contract constraints
Legal method calls
What methods exist? With what parameters?
Can I pass a null as a parameter?
Legal sequences of method calls
Can I call any methods after I call close()?
What are the consequences if I don’t call close()?
Aliasing constraints
Once I pass my reference as a parameter, can I use it
again?
Static type checking
Wanted: guaranteed safety properties without running
the program
Types express what is legal, type checking enforces this
Java types do not convey everything that is important!
No specification of legal sequences
No specification of alias restrictions
No specification of concurrency restrictions
... etc.
So, we must informally document these constraints...
Did you read the fine print?
Excerpt from the PC Pitstop Software EULA:
SPECIAL CONSIDERATION
A special consideration which may include financial
compensation will be awarded to a limited number of
authorized licensee who read this section of the license
agreement and contact PC Pitstop at
consideration@pcpitstop.com. This offer can be withdrawn
at any time.
CAN_REMOVE {
remove() -> DEFAULT
}
while(it.hasNext()) {
Object a = it.next();
if(it.hasNext()) {
// what can we do here?
}
}
CAN_REMOVE {
remove() -> DEFAULT
hasNext() :: true -> NEXT_AVAILABLE_CAN_REMOVE
}