Hoc prohibit native features

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

Hoc prohibit native features is an AI agent skill from the GitHub repository openreachtech/hora-skills-ort-core. The native JavaScript features this library does not use, and why each is refused. Use as the criterion whenever code is about to reach for a native feature — `Map`, `Object.freeze()`, a `#` private member, a decorator, an object literal handed across scopes — or when reviewing code that does. A cla

How to use this skill

Skill file

Prohibit Native Features

The native features below are not used. Each one does a job this library's design already does

another way — immutability held by convention, a class that can be overridden in part — and

each costs something that way of working depends on.

(/hoc-classes-principles): what it rules out is part of what a property is, so it stays

with the definition of a property.

Object literals shared across scopes

An object literal is not shared across scopes on the strength of its shape. Built and used

up where it stands, a literal is fine. Handed out — returned, kept in a property, passed on to

be read elsewhere — it becomes a value whose reader trusts only that it has the right keys, and

what holds values is a class. Why a shape is not a value belongs to the class design principles

convention (/hoc-classes-principles).

the callee destructures it on arrival into separate parameters; nothing keeps it as a value.

reader still holds a nameless object.

// NG: a literal leaves the method, and its reader relies on the shape
computeTotalPerRegion () {
  return this.extractKeptSales()
    .reduce((totals, it) => ({
      ...totals,
      [it.region]: (totals[it.region] ?? 0) + it.amount,
    }), {})
}

// OK: what leaves the method is an instance of a class
computeRegionTotals () {
  const keptSales = this.extractKeptSales()

  return [
    ...new Set(
      keptSales.map(it => it.region)
    ),
  ]
    .map(region => this.Ctor.RegionTotalCtor.create({
      region,
      amount: keptSales
        .filter(sale => sale.region === region)
        .reduce((total, sale) => total + sale.amount, 0),
    }))
}

`Map`

table keyed by objects, and that job is WeakMap's: it holds a value against an object, and lets

the value go when the object is released. Map holds on to both, and gives nothing WeakMap

does not.

special reason.** No such reason is known today. The exception is left open only because one

may be met some day, however unlikely; it is not a door to reach for. A Map written there

carries its reason beside it, and the use stays inside that module.

aggregate that dodges the prohibitions on reassignment and on mutable objects, and it is the

only way Map has been reached for in application code.

not enumerate; where the keys have to be walked, hold them in an Array and traverse that.

function and return it, or generate a new instance via a factory method).

changed, a decoded payload — and its get() and has() are called. Nothing is written to it:

the map is not the receiver's to change.

CBOR map whose keys are integers, is written as one there.

Looking up by a string or a number

A lookup by a string or a number is what Map has been reached for, and it needs no Map. The

elements are held in an Array, and the one asked for is found with find().

the collection. Built within a method, it walks every element to answer one lookup, which is

already more than find() does. Kept beside the collection, it is a copy built again whenever

the elements change — the mutable aggregate refused above, reached by another route.

many that walking them is worth a second structure, and a Map is not reached for to make it

faster.

there.

// NG: an index object is built again from the elements on every lookup
extractMemberByEmail ({
  email,
}) {
  const memberPerEmail = Object.fromEntries(
    this.members.map(it => [it.email, it])
  )

  return memberPerEmail[email]
}

// OK: the elements are asked directly
extractMemberByEmail ({
  email,
}) {
  return this.members
    .find(it => it.email === email)
}

Native private members (`#x`)

Under an immutable property design, there is no work left that is unique to #private. One might initially evaluate abandoning it as "the biggest cost," but within this system it is not even a cost.

representation to refactor) is carried by references = the contract. Coupling to a member not in the references is

outside the contract, so the author's freedom to refactor is guaranteed by the contract without waiting for #. (2)

Secrecy (hiding the value itself, e.g. keys/passwords) is independent of immutability, but JS's # is not a

security boundary against memory dumps/debuggers, so it is not a guarantee # can reliably provide — that is the

job of a separate layer (encryption, not retaining).

Therefore soft-private (this._x) is visible and correct. Do not use #private unless a human explicitly specifies it.

#alpha is member notation, not a request for a native # field — see the property-definition

convention.

`static #x`

The reason not to use static #X is that the reason not to use native private (a subclass cannot read it, which blocks extension and substitution through inheritance — see above) holds for static as well, and its impact is broader: when a static method refers to this.#X, a call through a derived class throws a TypeError.

// NG: a static native private cannot be used from a derived class
class Base {
  static #pool = new WeakMap()

  static ensure (key) {
    return this.#pool.has(key)
  }
}
class Derived extends Base {}

Derived.ensure(key)
// TypeError: Cannot read private member #pool from an object whose class did not declare it

Decorators

`Object.freeze()`

Object.freeze() is not used. Immutability is held by the conventions, so freezing guards a

hazard this system has already removed — the same reasoning that sets native private aside above.

property of the same name to the instance, and a frozen object takes no new property: the spy

fails with TypeError: Cannot add property …, object is not extensible. A subclass patching the

instance is shut out the same way.

assembled (see /hoc-properties); a frozen one cannot be.

// NG: the instance is frozen, so a test cannot spy on it
constructor ({
  value,
}) {
  this.value = value

  Object.freeze(this)
}
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.