+ All Categories
Home > Software > Usage contracts in a nutshell

Usage contracts in a nutshell

Date post: 01-Dec-2014
Category:
Upload: kimmens
View: 147 times
Download: 0 times
Share this document with a friend
Description:
Developers often encode design knowledge through structural regularities such as API usage protocols, coding idioms and naming conventions. As these regularities express how the source code should be structured, they provide vital information for developers (re)using that code. Adherence to such regularities tends to deteriorate over time when they are not documented and checked explicitly. Our uContracts tool and approach allows to codify and verify such regularities as ‘usage contracts’. The contracts are expressed in an internal domain-specific language that is close to the host programming language, the tool is tightly integrated with the development environment and provides immediate feedback during development when contracts get breached, but the tool is not coercive and allows the developer to decide if, when and how to correct the broken contracts (the tool just highlights the errors and warnings in the integrated development environment). In spirit, the approach is very akin to unit testing, except that we do not test behaviour, but rather verify program structure. The tool, of which some screenshots can be found below, was prototyped in the Pharo dialect of the Smalltalk programming language. This work was conducted by Angela Lozano, Andy Kellens and Kim Mens.
7
USAGE CONTRACTS* KIM MENS UNIVERSITÉ CATHOLIQUE DE LOUVAIN (UCL) DAGSTUHL SEMINAR – “THE FUTURE OF REFACTORING” LIGHTNING TALK – 21.05.2014 JOINT WORK WITH ANGELA LOZANO ANDY KELLENS * SLIDES LARGELY BASED ON AN EARLIER PRESENTATION MADE BY ANGELA LOZANO
Transcript
Page 1: Usage contracts in a nutshell

USAGE CONTRACTS*KIM MENS UNIVERSITÉ CATHOLIQUE DE LOUVAIN (UCL)

DAGSTUHL SEMINAR – “THE FUTURE OF REFACTORING” LIGHTNING TALK – 21.05.2014

JOINT WORK WITH ANGELA LOZANO ANDY KELLENS

* SLIDES LARGELY BASED ON AN EARLIER PRESENTATION MADE BY ANGELA LOZANO

Page 2: Usage contracts in a nutshell

USAGE CONTRACTS

• We want a tool that allows encoding such regularities and offering immediate feedback on violations of such structural source-code regularities

• The tool should be proactive (violations reported ‘on the fly’ during coding)

• The tool should be “developer-friendly” (like unit testing but for usage expectations)

• desired regularities expressed in the same programming language

• tight integration with the integrated development environment

• not coercive

/** * Deactivates the tool. This method is called whenever the user switches to another tool * Use this method to do some clean-up when the tool is switched. * Subclassers should always call super.deactivate. * An inactive tool should never be deactivated. */public void deactivate() {

if (isActive()) {if (getActiveView() != null) {

getActiveView().setCursor(new AWTCursor(java.awt.Cursor.DEFAULT_CURSOR));}getEventDispatcher().fireToolDeactivatedEvent();

}}

Page 3: Usage contracts in a nutshell

METAPHOR

Provider

Consumer

uses

UsageContract

describesexpectations

of

shouldcomplywith

Page 4: Usage contracts in a nutshell

EXAMPLE

copyFrom: anEntity within: aVisitor

copyFrom: anEntity within: aVisitor super copyFrom: anEntity within: aVisitor ...

inheritsfrom

All overriders of copyFrom:within: should start with a super call

describesexpectations

of

shouldcomply

with

Page 5: Usage contracts in a nutshell

EXAMPLE

copyFrom: anEntity within: aVisitor

copyFrom: anEntity within: aVisitor super copyFrom: anEntity within: aVisitor ...

inheritsfrom

All overriders of copyFrom:within: should start with a super call

describesexpectations

of

shouldcomply

with

classesInFAMIXSourcedEntityHierarchycopyFromWithinWithCorrectSuperCall

FAMIXSourcedEntityContract

EContract

classesInFAMIXSourcedEntityHierarchy <liableHierarchy:#FAMIXSourcedEntity>

copyFromWithinWithCorrectSuperCall <selector:#copyFrom:within:>contract require: condition beginsWith: (condition doesSuperSend: #copyFrom:within:) if: (condition isOverridden)

Liable entity

Contract term

Contract conditions

Page 6: Usage contracts in a nutshell

UCONTRACTS : THE LANGUAGE

classesInFAMIXSourcedEntityHierarchycopyFromWithinWithCorrectSuperCall

FAMIXSourcedEntityContract

EContract

classesInFAMIXSourcedEntityHierarchy <liableHierarchy:#FAMIXSourcedEntity>

copyFromWithinWithCorrectSuperCall <selector:#copyFrom:within:>contract require: condition beginsWith: (condition doesSuperSend: #copyFrom:within:) if: (condition isOverridden)

Liable entity

Contract term

Contract conditions

Liable classes •liableClass: regExp / exceptClass: regExp •liableHierarchy: className / exceptHierarchy: className •liablePackage: regExp / exceptPackage: regExp

Liable methods •selector: regExp / exceptSelector: regExp •protocol: regExp / exceptProtocol: regExp •/ exceptClass: className selector: selector

Contract terms •require: condition •suggest: condition •require: condition if: anotherCondition •suggest: condition if: anotherCondition

Contract conditions •assigns: regExp •calls: regExp •references: regExp •returns: expression •doesSuperSend: regExp •doesSelfSend: regExp •inProtocol: regExp •isOverridden: selector •isOverridden •isImplemented: selector •custom: visitor

!•and: cond1 with: cond2 •or: cond1 with: cond2 •not: cond !

•beginsWith: cond •endsWith: cond •does: cond1 after: cond2 •does: cond1 before: cond2

Page 7: Usage contracts in a nutshell

UCONTRACTS : THE TOOL


Recommended