SECTION 13 / 15

Write a contract: inputs, result and allowed changes

2 min read
On this page 2 headings

A procedure’s interface is the boundary between caller and helper. Its contract states what the caller supplies, what the helper returns, and what changes to the input are permitted. The implementation is how the helper performs the work.

A reusable procedure needs a clear agreement about inputs, outputs and side effects.
A reusable procedure needs a clear agreement about inputs, outputs and side effects.
Keep the original word sequence when following the course’s simplified backwards-search rule.
Keep the original word sequence when following the course’s simplified backwards-search rule.
Contract partExample: SumMarks
InputsA nonempty or empty score dataset, a valid Gender value, and a valid numeric field name.
ResultThe sum of that field for all matching records; zero if no rows match.
Data integrityDo not change marks, IDs or records.
Order policyPreserve the original order, or explicitly document if order may change.

Replace the method while preserving the promise

If you improve one procedure, every call can benefit without rewriting the main code. But the new version must preserve the agreed input meaning, result and side-effect policy. Returning the same number is insufficient if the new version unexpectedly destroys a carefully sorted deck.

Some tasks depend on the sequence

For a text-search exercise, a pronoun such as He can be associated with a preceding name by a backwards-search rule. In the supplied paragraph, Swaminathan occurs before the later He. Shuffling the word records destroys the “before” relation. Preserve SerialNumber and order when using such a rule.

Nearest preceding name is a simplified course heuristic, not a complete English-language pronoun resolver. A PartOfSpeech label of Noun alone does not prove that a word is a person’s name; apply the rule specified by the task.

Example contract for a text search:
Input: a pronoun position and the ordered words before/after it
Output: a matching name under the stated search rule, or "not found"
Allowed changes: none to words, IDs, or order

A reading-only loop over table rows need not move the rows at all. Card-pile pseudocode makes movement visible; real implementations must still make their data-change promises explicit.

Try it yourself

What is the difference between interface and implementation?

Show explained answer

The interface is the agreement the caller relies on. The implementation is the internal method used to meet it.

Try it yourself

May a faster procedure violate its old order guarantee?

Show explained answer

No. It must preserve the full contract, including allowed changes.

Try it yourself

Why is shuffling word cards dangerous for a backwards search?

Show explained answer

It changes which words come before the pronoun and can change the answer.

Try it yourself

Must every procedure return a useful number?

Show explained answer

No. Some procedures act by updating or rearranging data.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑