Role of Specifications in Programming Language Technologies and Paradigms - Prof. Atif M. , Study notes of Programming Languages

The importance of software specifications in programming language technologies and paradigms. It explains how specifications act as a contract between users and providers, helping to write and maintain code. The document also covers the differences between formal and informal specifications and their advantages and disadvantages.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-76j
koofers-user-76j 🇺🇸

9 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Documentation
Feb. 20, 2007
2
Software Specifications
A specification defines the behavior of an abstraction
•This is the contract between user and provider
Provider’s code must implement the specification
Providers are free to change the implementation
So long as the new code meets the specification
Users who depend only on specification won’t have trouble
Don’t rely on implementation
Black box testing essentially checks compliance of an
implementation with its specification
3
Good Specifications are Hard to Write
Very difficult to get people to write specifications
Even harder to keep them up to date
Having specifications in a separate document from
code almost guarantees failure
Rationale for Javadoc: extract a standalone
specification from the code and embedded comments
Hard to accurately and formally capture all
properties of interest
Always finding important details not specified
4
Specifications Help You Write Code
Lots of subtle algorithms and data structures
Internal specs/invariants vital to correct implementation
Example: Binary Search Tree
All nodes reachable from left child have smaller key
than current node
All nodes reachable from right child have larger key
than current node
pf3
pf4
pf5

Partial preview of the text

Download Role of Specifications in Programming Language Technologies and Paradigms - Prof. Atif M. and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Feb. 20, 2007DocumentationSpring 2007

Software Specifications

A specification defines the

behavior

of an abstraction

This is the

contract

between user and provider

  • Providers are free to change the implementation– Provider’s code must implement the specification
  • So long as the new code meets the specification
  • Users who depend only on specification won’t have trouble
  • Don’t rely on implementation

implementation with its specificationBlack box testing essentially checks compliance of an

3

Good Specifications are Hard to Write

– Even harder to keep them up to dateVery difficult to get people to write specifications

– Rationale forcode almost guarantees failureHaving specifications in a separate document from

(^) Javadoc

: extract a standalone

specification from the code and embedded comments

– Always finding important details not specifiedproperties of interestHard to accurately and formally capture all

Specifications Help You Write Code

– Internal specs/invariants vital to correct implementationLots of subtle algorithms and data structures

– All nodes reachable from left child have smaller keyExample: Binary Search Tree

than current node

  • All nodes reachable from right child have larger key than current node

5

• Specifications Help You Maintain Code

– Perhaps many different people have modified this code– Often originally written by somebody elsemodifying previously written codeIn the real world, much coding effort goes into

specifications are the way to avoid a messDocumenting and respecting key internal

pitchforkscustomers storm the office with torches andspecifications are the way to avoid having yourDocumenting and respecting key external

Formal vs. Informal Specifications

static

(^) int (^) find(int[]

(^) d,int

(^) x)

  • An informal specification (^) If the array

(^) d is sorted, and some element of the array

(^) d is equal to

x, then find() returns the index of

(^) x ……

  • A formal specification (^) (for all i, 0 < i <

(^) d .length,

(^) d [i-1] < (^) d [i]

and there exists j, 0 <= j <

(^) d .length, such that

(^) d [j] == (^) x )

implies find(d,x) = j ……

Note: These specs assume array has no duplicates

7

Advantages and Disadvantages

– Automated tools can check some specifications– Forces you to be very clearFormal specifications

  • Either at compile-time (static checking) or run-time (dynamic checking)

– Some important properties are hard to express formallyInformal specifications

  • Sometimes don’t have the necessary formal notation• Sometimes just difficult
  • Some people are intimidated by formal specs

Types of External Specifications

– Post-conditions/effects: What is must be true after call– Pre-conditions/requires: What must be true before callSpecifications on methods

  • Often relates final values to initial values

// // postcondition:// precondition: the array d is sorted returnValue >= 0 && d[returnValue] == x

or (returnValue == -1 && x does not occur in d)

static int find(int d[], int x);

13

Javadoc

Integrates documentation into source code as comments

Will generate an external specification

public class MyClass { / Javadoc Comment for this class / /* Javadoc Comment for field**

(^) text

(^) */

/ Javadoc Comment for methodString text;**

(^) setText

@param t Javadoc comment for parameter

(^) t

public void setText(String t) {...}/*

}

Javadoc example

**/**** (^) Given

(^) a (^) sorted

(^) array, returns the

(^) index

into (^) the array of

(^) the (^) given

(^) element,

otherwise

(^) returning -1.

@param d

(^) array

(^) to search in,

(^) assumed

(^) sorted

@param x

(^) the element

(^) to search

(^) for

@returns

(^) i (^) >= (^0) (^) when

(^) d[i]

(^) == (^) x, and

(^) -1 when

x does (^) not occur in

(^) d

public/*

(^) static int

(^) find(int d[],

(^) int (^) x) (^) {

}

15

Javadoc example: HTML

A Few Javadoc Tags

– @version– @authorSpecial tags for classes

– @exception– @return– @paramSpecial tags for methods

– @seeReference to another element

17

Object Modeling Technique (OMT)

– Graphical representation of OO relationships

(^) Class diagrams

(^) show the static relationship between

classes

Object diagrams

(^) represent the state of a program as

series of related objects

Interaction diagrams

(^) illustrate execution of the

program as an interaction among related objects

Classes

19

Object instantiation

Subclassing and Abstract Classes