Hoc accessors

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

Hoc accessors is an AI agent skill from the GitHub repository openreachtech/hora-skills-ort-core. Conventions for class accessors, getters and setters alike. Use when adding or reviewing an accessor — a getter that holds a dependency included — and when about to write a getter that counts, filters or builds the value it returns. Which getter a dependency takes, and the factory method beside it,

How to use this skill

Skill file

Classes: Members / Accessors

This summarizes conventions related to class accessor (getter / setter) definitions.

Setters are prohibited

reassigned). A setter would allow reassignment after creation, breaking the premise of immutability.

be defined as needed. This is not a reassignment of a class property but the definition of Proxy behavior, and

does not conflict with the intent of immutability.

The `#get:Ctor` getter

its name is reserved, belong to /hoc-classes-ctor.

Getters that wire dependencies

it instantiates, a getter returning the module as-is for one it uses directly. Which getter a

dependency takes, and the factory method beside it, belong to /hoc-wire-dependencies.

class and nothing else; the .create(...) call belongs in the factory method, never in the

getter.

Do not write branching inside a getter

// OK: this.entity is the receiver, one property per line. undefined is resolved to null with ?? null
get authorId () {
  return this.entity.comment
    ?.author
    ?.id
    ?? null
}
// NG: the leading `this.entity.comment ?.author` is duplicated across multiple getters
get authorId () {
  return this.entity.comment
    ?.author
    ?.id
    ?? null
}

get authorName () {
  return this.entity.comment
    ?.author
    ?.name
    ?? null
}

// OK: extract the common leading part into a getter, and use it as the receiver from then on
get author () {
  return this.entity.comment
    ?.author
    ?? null
}

get authorId () {
  return this.author?.id
    ?? null
}

get authorName () {
  return this.author?.name
    ?? null
}

Do not call a method from a getter

// NG: calling a method from a getter. The caller's this.authorLabel looks like a property, yet formatting runs behind it
get authorLabel () {
  return this.buildLabel(this.author)
}

// OK: keep the getter to a property reference, and let the caller call the formatting as a method
get author () {
  return this.entity.comment
    ?.author
    ?? null
}

buildAuthorLabel () {
  return this.buildLabel(this.author)
}

throwing is out of scope, whichever of the two throws the error-handling convention gives it.

That covers the one that reaches the module's own error class through a factory method —

a call inside a getter, and the exception would be worth nothing without it, since the getter

never returns. What the throw looks like follows the error-handling convention.

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.