Skip to content
The AI Agent Factory

14.3 Classes and objects

Approved by
Panaversity ·
11 min read

In everyday life. A builder works from a blueprint for a house. The blueprint is not a house. Each house built from it is one real thing, with its own address and paint color. All have the same rooms.

What you will learn. How to read a class: the data each object holds, and the job of each method.

Why it matters. Much of the code an agent writes, and most code a company publishes for its service, is built this way.

What is a class, and what is an object?

A blueprint, and one thing built from it.

See a small one work. This class is the blueprint for one invoice. Two objects are built from it, as two houses are built from one blueprint, and each holds its own number and amount. The comments name the parts. Predict both lines.

class Invoice {
  // its two fields
  number: string;
  cents: number;

  // the constructor: it runs
  // when an object is built,
  // and fills in its fields
  constructor(number: string, cents: number) {
    this.number = number;
    this.cents = cents;
  }

  // a method: "this" is the
  // object it was called on
  needsSecondApprover(): boolean {
    // is this one's amount
    // over $10,000.00?
    return this.cents > 1000000;
  }
}

// build one object
const first = new Invoice("INV-8301", 482500);
// and another, with its own data
const second = new Invoice("INV-8303", 1290000);
console.log(first.number, first.needsSecondApprover());
console.log(second.number, second.needsSecondApprover());
INV-8301 false
INV-8303 true

Each object holds its own cents, so the same method gives two different answers. These words name the parts.

PartWhat it isIn the code above
ClassA blueprint for a kind of thingInvoice
Object, or instanceOne thing built from itfirst and second
FieldsThe data each object holdsnumber, cents
ConstructorThe method that runs when an object is built and fills in its fieldsTakes a number and an amount
MethodsFunctions that work on that object's dataneedsSecondApprover()

Inside a method, the object is this in TypeScript and self in Python.

How do I read a class I did not write?

By its fields, and by the one-sentence job of each method.

At work. The agent reorganized Brightline's scripts into two small classes. VendorDirectory looks up vendor names by id. InvoiceBook holds the invoices and a directory, and answers questions about them. Ana lists each class's fields, and says what each method does in one sentence.

Read a short version of VendorDirectory the same way: one field, and one method. The method's last line uses the or default from 13.5, and puts a value inside text, as its comment says. The mark at the start of the field's name, # in TypeScript and _ in Python, says the field is private: it is for the class's own methods only. Predict both lines.

class VendorDirectory {
  // its one field: the names,
  // kept by id
  #byId: Record<string, string>;

  constructor(byId: Record<string, string>) {
    this.#byId = byId;
  }

  // its one method: the name
  // under this id, or this text
  // if there is none.
  // `...${id}` is text that
  // holds a value
  name(id: string): string {
    return this.#byId[id] || `Unknown vendor ${id}`;
  }
}

const directory = new VendorDirectory({ V005: "Hocking Valley Pallets" });
console.log(directory.name("V005"));
console.log(directory.name("V099"));

Both print:

Hocking Valley Pallets
Unknown vendor V099

Hold another object, or extend it?

Usually hold it. With composition, a class holds another object as a field and uses it. With inheritance, a class extends another and becomes a kind of it, taking all its methods. An InvoiceBook is not a kind of vendor directory. It has one. When an agent's class extends another, ask whether "is a kind of" is really true.

Here is a short version of InvoiceBook, with the directory from the block above. Predict what it prints.

class InvoiceBook {
  // it has a directory
  vendors: VendorDirectory;

  constructor(vendors: VendorDirectory) {
    this.vendors = vendors;
  }
}

const book = new InvoiceBook(directory);
// the book asks its directory
console.log(book.vendors.name("V005"));

Both print:

Hocking Valley Pallets

Extending would be written class InvoiceBook extends VendorDirectory in TypeScript, or class InvoiceBook(VendorDirectory) in Python. That line would claim a book is a kind of directory.

Try it

The file in 14-3 is the small Invoice class. Run it and investigate, as in 13.1.

Modify. Send: "Change the second invoice's amount to exactly 1000000 cents. Don't run it yet." Say what each line will print. Then ask the agent to run it.

Make. Send: "Add a status field to the class and a method that answers whether the invoice is open. Print the answer for one open invoice and one paid invoice. Don't run it yet." Find the new field and the new method. Say what it will print, and then ask the agent to run it.

What you should see. After your change, both invoices answer false. Exactly $10,000.00 is not over $10,000.00.

The operating rule: read a class by its fields and the one-sentence job of each method. Prefer an object that holds another to one that extends it.

Figure 14.4. An InvoiceBook holding its invoices and a VendorDirectory. Its methods are open, openTotalCents, dueDateOf and dueBy, and dueBy calls the directory's name(id) method to add vendor names. A side panel contrasts has a, which is composition, with is a kind of, which is inheritance: an invoice book is not a vendor directory.

Figure 14.4. Composition. The book has a directory and asks it for names. It is not a kind of directory.

Next. In 14.4, you decide what your program lets in.

Check yourself

Question 1 / 4

0 answered

What does a constructor do?

Owner
Panaversity