Hoc properties

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

Hoc properties is an AI agent skill from the GitHub repository openreachtech/hora-skills-ort-core. Conventions for class properties — where they are set, whether they may change, and what may hold them. Use when declaring, assigning or reviewing a property of a class, including when a note writes a member as `#alpha`. `Map` and native private belong to the native-feature prohibition convention; a

How to use this skill

Skill file

Classes: Members / Properties

This summarizes conventions related to class property definitions.

Set properties on `this` within the constructor

are not used, belong to /hoc-classes-principles.

outside, so a test can read it directly.

Classes are immutable / property reassignment is prohibited

the property's object path** — this.state.count += 1 as much as this.state = .... ESLint

enforces the direct form; what lies under the path is held by this rule alone.

no need to make it native private for encapsulation purposes (for why, see /hoc-prohibit-native-features).

// NG: reassigning a property after creation
scalar.normalizedValue = anotherValue

// NG: reassigning under a property's object path
this.state.count += 1

// OK: if a different state is needed, generate a new instance via a factory method
const next = Scalar.create({
  normalizedValue: anotherValue,
})

A collection is built, then used all at once

**Code that makes an instance mutable is prohibited, uniformly — the reassignment prohibition is

one means to that, and a collection is no exception.** The one thing an Array or a Set allows is

being built: elements are added while the collection is assembled, and the finished collection is

then used all at once, every element together. A collection added to or taken from between reads,

while the instance is in use, is state that changes; the change goes to a new instance through

the factory method instead.

This holds wherever the collection sits — a property, or anything under its object path — and a

Set is held to it exactly as an Array is.

runs until #buildXxxx() is called on the values the collection property holds. Until then,

elements are added to it; from that call on, it is used all at once like any other.

```javascript

// OK: the builder's collection is edited until #buildQuery() uses it

addCondition ({

condition,

}) {

this.conditions.push(condition)

}

buildQuery () {

return this.conditions

.map(it => it.toClause())

.join(' AND ')

}

```

settles. Taking an individual element out — [n] on an Array, values().next() or

[...set][0] on a Set — is not using the collection all at once.

and replacing what sits there with splice(index, 1, newEntity) is array[index] = newEntity

under another name. The changed collection is another array, built with map().

class, in a member that answers the count — get entityCount () — and whether it is empty is

answered the same way, by isEmpty (). A caller handed the collection asks the holder rather

than reading its length itself.

being used as an array; what it holds is a number.

// NG: the collection grows while the instance is in use
addEntity ({
  title,
}) {
  this.entities.push(
    TodoEntity.create({
      title,
    })
  )
}

// OK: the changed collection goes to a new instance
addEntity ({
  title,
}) {
  return this.Ctor.create({
    entities: [
      ...this.entities,
      TodoEntity.create({
        title,
      }),
    ],
  })
}

// NG: the element at a position is replaced in place
markEntityDone ({
  title,
}) {
  const index = this.entities.findIndex(it => it.title === title)

  this.entities.splice(index, 1, this.entities.find(it => it.title === title).generateDone())
}

// OK: another array, held by a new instance
markEntityDone ({
  title,
}) {
  return this.Ctor.create({
    entities: this.entities
      .map(it => (
        it.title === title
          ? it.generateDone()
          : it
      )),
  })
}

// NG: the caller reads the length of a collection it was handed
const count = todoList.entities.length

// OK: the holder answers its count and its emptiness
get entityCount () {
  return this.entities.length
}

isEmpty () {
  return this.entities.length === 0
}

`Map` is not used

belongs to /hoc-prohibit-native-features.

The meaning of `#alpha` notation and the treatment of native private

(Member notation follows /hoc-classes-member-notation.)

That rule, and why it holds, belong to /hoc-prohibit-native-features. It is read here for what it

means when a property is named.

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.