Sub topic 14
Design Clear Method Contracts
A method contract is an agreement about what a method expects and what it does. A method is a named piece of code you can call to do a job. A clear contract lets people use a method without reading all its code.
A parameter is a variable listed in a method’s declaration. An argument is the value passed to that parameter in a call. A return value is the result a method sends back to its caller.
What to Include in a Contract
| Part | Question to answer |
|---|---|
| Inputs | What arguments are allowed? |
| Result | What does the method return? |
| Changes | Does it change anything the caller can see? |
| Errors | What happens if an argument is not allowed? |
A change the caller can see, other than the return value, is called a side effect. Changing an array or printing a message is a side effect. An array is a fixed-length collection of values.
Example: Add Values Without Changing Them
The following method adds the values in an array. It returns a long, a whole-number type that can hold larger values than an int. An int is also a whole-number type.
The contract says that the array must not be null. In Java, null means a reference does not point to an object. If the argument is null, the method throws an exception, a signal that something went wrong.
class ContractDemo {
/**
* Adds the numbers without changing the array.
* @param values the numbers to add; must not be null
* @return the sum as a long, or 0 if the array is empty
* @throws IllegalArgumentException if values is null
*/
static long sumValues(int[] values) {
if (values == null) {
throw new IllegalArgumentException("values must not be null");
}
long total = 0;
for (int value : values) {
total += value;
}
return total;
}
public static void main(String[] args) {
int[] numbers = {2, 3, 4};
long result = sumValues(numbers);
System.out.println(result);
System.out.println(numbers[0]);
System.out.println(sumValues(new int[0]));
}
}Save the code as ContractDemo.java, compile it with javac ContractDemo.java, and run it with java ContractDemo. It prints 9, 2, and 0 on separate lines. The second line shows that the first array value is still 2. The last line shows the result for an empty array.
The comment above sumValues records its contract. The method checks for null before reading the array. IllegalArgumentException tells the caller that an argument was not allowed. The method also uses a long total, so adding values does not overflow the smaller int range.
Be Clear About Changes
Java passes a copy of each argument’s value. When the argument refers to an array, the copy still refers to the same array. A method can therefore change the array’s values, and the caller will see those changes.
That is why the promise “without changing the array” matters. Someone can safely use sumValues(numbers) and keep using numbers afterward.
A method cannot replace the caller’s array variable just by assigning a different array to its parameter. If the caller needs a replacement array, the method can return it, and the caller can assign the result.
Make the Contract Easy to Follow
- Give the method a name that describes its job.
sumValuesis clearer thandoStuff. - Keep the job focused. A method that adds numbers need not also print them.
- State what happens for special inputs, such as an empty array or
null. - Describe changes as well as results. Do not surprise the caller by changing an argument.
- Keep the code and the written contract in agreement. If either changes, check the other.
Exercise
You design a method that doubles each value in an array by changing that array. What should its contract say? Can assigning a new array to the parameter replace the caller’s array variable?
Show answer
State that the method changes the array, what it returns, and how it handles inputs such as null or values that are too large to double. Assigning a new array to the parameter cannot replace the caller’s variable. To provide a replacement, return the new array and let the caller assign it.