Let's look an example of some imperative code:
type Dividend struct { Val int}func (n Dividend) Divide(divisor int) int { return n.Val/divisor}func main() { d := Dividend{2} fmt.Printf("%d", d.Divide(0))}
What is our contract in the preceding code?
The contract is our method's signature: func (n Dividend) Divide(divisor int) int
What three questions must our contract answer?
- What does our contract expect?
- Answer: It expects the following:
- The Dividend.Val to be populated with an int
- The divisor to be an int
- What does our contract guarantee?
- Answer: It promises to return an integer
- What does the contract maintain?
- Answer: Not applicable in this simple case
What happens when we run the preceding ...