Hoc classes principles

From openreachtech/hora-skills-ort-core · View interactive version

Hoc classes principles is an AI agent skill from the GitHub repository openreachtech/hora-skills-ort-core. Principles of class design — what a class has to hold to exist at all, and the premises the other class conventions rest on. Use when deciding whether something should be a class, when reaching for a class field or a plain object, or when the reason behind a class convention is in question. Native f

How to use this skill

Skill file

Classes: Principles

This summarizes the principles for class property declaration and design.

Core principle: Do not create classes without properties

exceptions and reasons belong to /hoc-classes-prohibits. This skill takes it as the core

principle and builds the surrounding design conventions on top of it.

The five points of the system (a bundle of premises)

  1. Deep immutability — Do not reassign after construction; keep nested values (e.g. instances of other immutable classes) immutable too. The collection types Array / Set are not deep-frozen, because they are built: elements are added while the collection is assembled, and the finished collection is then used all at once. The reasons are that "the set of retained items" is premised on being treated as equal regardless of count, and that building supports the Builder Pattern, whose editing period runs until #buildXxxx() is called. How a collection's value may be used belongs to /hoc-properties, and why Map gives way to WeakMap to /hoc-prohibit-native-features.
  2. Property = a constructor argument of the same name — Every property that stores a value is passed as a

constructor argument of the same name. This makes the set of property names a class occupies appear in the

constructor signature itself, so hidden fields cannot exist in principle.

  1. Constructor-only — Only this.xxx = xxx inside the constructor counts as a property (see "Definition of 'property'" below).
  2. Treat references as the contract — The published API references fix the public surface. Direct access to a member not in the references is "undocumented = outside the contract," and breaking after coupling to it is the caller's responsibility. This is the stance that "reaching for a member outside the contract is the same as throwing a microwave at a beast" — a use outside the contract is not the designer's responsibility.
  3. Enumerating instances is prohibited — Enumerating a class instance that has behavior and invariants via Object.keys / Object.entries / for...in / spread {...instance} / Object.assign is treated as hack code. It is a category error that treats a behavior-bearing object as a POJO/dictionary, and a clearly bad design practice that drops the prototype (methods) to make a degraded copy.

Definition of "property"

Reasons:

(and its argument signature). If class fields are allowed, then even adding a member-ordering rule of "fields go

first" still leaves two places to read: the leading field group and the computed this.x =. The real ground for

going as far as prohibition is not "scattered and hard to find" but that it guarantees "strictly one location"

(if it were merely "hard to find," an ordering rule would suffice, and someone would point that out).

// NG: mixing default + override (split-brain)
class Foo {
  x = 1              // default declaration
  constructor (x) {
    if (x !== undefined) {
      this.x = x     // override; the field initializer dies
    }
  }
}

Handling of `static` fields

/hoc-prohibit-native-features.

This is because none of the three grounds for the prohibition apply to static.

Why static #X is not used either belongs to /hoc-prohibit-native-features.

What to put in a `static` field

What not to use

The native features this system does not use — Map, Object.freeze(), native private members

and decorators — belong to /hoc-prohibit-native-features, with the reason for each. What is

left here is the plain object, which is a matter of design rather than of a language feature.

Plain objects

A plain object is avoided wherever it can be. What holds values is a class, so that the values

arrive with a name and with the members that work on them. The rule that draws the line — an

object literal is not shared across scopes on the strength of its shape — belongs to

/hoc-prohibit-native-features; what follows is why.

and keeps every other one as it was, which is how a hotfix, a variant or a test stand-in reaches

the one place it needs. A plain object offers nothing to override, and neither does a module of

exported functions — which is why a module is never used in place of a class either.

reaches through it or takes it apart, and the knowledge of its insides spreads to every place

that uses it. A class grows the member that answers — it.hasActiveAccount() in place of

it.account.isActive() — and the design keeps that room.

shape, and the name it gives lives in the annotation alone: at run time the value is the same

nameless object, with nothing on it to work on what it holds. Calling a plain object something

else through its type is writing HTML in nothing but <div> and <span> — a class attribute

does not make a <div> an <article>.

accepts any object of the right shape, so declaring the shape is enough to pass the check, and

the value object is never written. What it would have held — completing an entry, comparing two

of them — goes to whoever uses the object, and is written again in each place.

parsed and spread through the application as it is. An API is reached through the Payload /

Capsule / Launcher structure, and its response is always capsulized: what the application holds

is the capsule, never the parsed object. How the three are built belongs to each stack's API

client convention.

// NG: an entry held as a plain object
addEntry ({
  title,
}) {
  return this.Ctor.create({
    entries: [
      ...this.entries,
      {
        title,
        isDone: false,
      },
    ],
  })
}

// OK: an entry is an instance of its own class
addEntry ({
  title,
}) {
  return this.Ctor.create({
    entries: [
      ...this.entries,
      this.Ctor.TodoEntryCtor.create({
        title,
      }),
    ],
  })
}

Rules

immutability). Array / Set are built, then used all at once; how their value is used follows /hoc-properties, and

Map against WeakMap follows /hoc-prohibit-native-features

Proviso

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.