Grade 12 Computer Science · Designing classes for a problem

From the description to the classes

A starting point that works, and why it is only a starting point.

The important class that is not a thing at all

“A system managing the lending of books in a library”: one sentence, and now it must become classes. Book and reader come to mind at once. But the class most needed has a date, a due date and a mark of whether it was returned, and it is neither a book nor a reader. What is it, and how do you not miss it?

One receives a description of a problem in words — “a system managing the lending of books in a library” — and must get from it to classes. The question “where does one begin” looks entirely open, and it has a practical starting point that works nearly always.

The nouns in the description are the candidate classes, and the verbs the candidate operations.
// "a system managing the lending of books in a library"
//
//   nouns: system, lending, book, library
//   verbs: manage, lend, return, search, compute a fine
//
// first pass:  Book, Reader, Loan, Library
//   Loan is a noun that is easy to miss and hard to do without.

The noun most often missed

There is one kind of noun almost always forgotten, and it is usually the most important: the relationship between two things. A loan is not a book and is not a reader — it is the relationship between them, and it has data of its own: a date, a due date, whether it was returned. Whoever does not create a class for it is forced to squeeze that data into the book or the reader, and both are the wrong place. The sign identifying this: every time there is a datum belonging to a pair and not to either of them, there is a missing class there.

In the descriptionA candidate for…
A nounA class
A verbAn operation
A datum belonging to a pairA missing class

Example 1 — why this is only a starting point

  1. Some of the nouns are fields.
  2. And some of the verbs belong to one class.
  3. But there is something to start from.

Example 2 — why the loan needs a class

  1. It has data of its own.
  2. A date, a due date, whether it was returned.
  3. And they belong to the pair and not to either.
The list is a starting point: some candidates will turn out to be fields, and some verbs will merge.