# 【MIT软件构造】Designing Specifications

## 确定性（deterministic）VS不确定性（undeterministic）


static int findExactlyOne(int[] arr, int val)
requires:	val occurs exactly once in arr
effects:	returns index i such that arr[i] = val

static int findOneOrMore,AnyIndex(int[] arr, int val)
requires:	val occurs in arr
effects:	returns index i such that arr[i] = val


## 声明性（declarative）VS操作性（operational）

static String join(String delimiter, String[] elements)
effects:	append together the strings in elements, but at each step,
if there are more elements left, insert delimiter


effects: 	returns concatenation of elements in order, with delimiter
inserted between each pair of adjacent elements


## 功能强（Strong）VS功能弱（Weak）

//	原版
static int findExactlyOne(int[] a, int val)
requires:	val occurs exactly once in a
effects:	returns index i such that a[i] = val
// 先决条件变弱，结果说明不变
static int findOneOrMore,AnyIndex(int[] a, int val)
requires:	val occurs at least once in a
effects:	returns index i such that a[i] = val
//	先决条件不变，结果说明变强
static int findOneOrMore,FirstIndex(int[] a, int val)
requires:	val occurs at least once in a
effects:	returns lowest index i such that a[i] = va


## 一些忠告

1. 尽量独立功能：
static int sumFind(int[] a, int[] b, int val)
effects:	returns the sum of all indices in arrays a and b at which
val appears


1. 能区分不同情况：
static V put(Map<K,V> map, K key, V val)
requires:	val may be null, and map may contain null values
effects:	inserts (key, val) into the mapping, overriding any existing
mapping for key, and returns old value for key, unless none,
in which case it returns null


1. 功能性尽量强

1. 在某些时候，功能性应尽量弱
static File open(String filename)
effects:	opens a file named filename


1. 应抽象一点
static ArrayList<T> reverse(ArrayList<T> list)
effects:	returns a new list which is the reversal of list,
i.e. newList[i] = list[n-i-1] for all 0 ≤ i < n, where n = list.size()


©️2019 CSDN 皮肤主题: 游动-白 设计师: 上身试试