Ritenta
Back to roadmap

Lesson 1 of 7 · 3 min read

Objects and contracts

A course-notification service must respect each learner’s current preferences. Start by deciding who owns those rules, then use JavaScript and TypeScript to make the boundary visible.

Give changes a single entry point

A NotificationPreferences object can expose muteCourse(courseId) and allows(courseId). If its constructor stores a caller’s Set directly, that caller can later change the preferences through the original reference. Copy the set on input and return a detached snapshot when the settings screen needs one. An ECMAScript #mutedCourses field prevents direct access to the field at runtime. TypeScript private is checked by the compiler. Neither declaration prevents mutation through an alias you handed out. Test that changing a passed-in set after construction leaves allows unchanged.

Expose only the operation the caller needs

The reminder worker needs to know whether a notification is permitted. Its abstraction can be allows(courseId), with a documented answer for a course that has no explicit preference. Whether the implementation uses a set, a map of overrides or a record returned from storage can stay inside the module. A class is useful if several operations must preserve shared state. A closure or a function over an immutable settings value can express the same boundary when there is little behavior.

Trace the method lookup

Ordinary public instance methods declared on a JavaScript class live on its prototype. Instance state belongs to each object. If an instance has no own allows property, JavaScript looks for it along the prototype chain. A subclass method on the subclass prototype can be found before the parent’s implementation. The subclass cannot directly name its parent’s private field. Sharing a mutable Set on the prototype would share preferences across learners. Use Object.getPrototypeOf and Object.hasOwn to inspect where a method and an own field live.

Substitute by behavior

An ordinary object and a class instance can satisfy the same TypeScript policy shape. A worker can call allows without knowing which implementation it received. That is subtype polymorphism without requiring a shared base class. Both implementations still need to agree on observable behavior. A policy that returns true for every unknown course may violate a contract that requires explicit consent. Test that case against each implementation. Compose a policy with another function when you need one independent capability rather than a specialized kind of preference object.

Choose a public operation that expresses the rule, keep ownership of mutable data, and test the behavior callers rely on.

Long code lines scroll sideways →
class Recipient {
  #destination;

  changeEmail(raw, validateEmail) {
    const verified = validateEmail(raw);
    if (!verified) return false;
    this.#destination = verified;
    return true;
  }

  canReceiveEmail() {
    return this.#destination !== undefined;
  }
}

Sources