Hoc coding styles

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

Hoc coding styles is an AI agent skill from the GitHub repository openreachtech/hora-skills-ort-core. Coding style conventions for how code is laid out across lines, and the forms literals are written in. Use when writing or reviewing the layout of any expression or statement. Which statements may be written belongs to the statements convention; the layout of a JSDoc block, to the JSDoc convention.

How to use this skill

Skill file

Coding Styles

Conventions related to coding style.

Chop down immediately before a binary operator

// NG
const total = alpha + beta + gamma

// OK
const total = alpha
  + beta
  + gamma

Chop down `??` as a branch

// NG: applying the literal exception and keeping it on the same line
const author = this.entity.comment?.author ?? null

// OK: chop down immediately before `??` as a branch
const author = this.entity.comment
  ?.author
  ?? null

When an if statement's condition spans multiple lines, apply consistency chopping down to `()`

(Break the line immediately after (, place each condition on its own line, and place ) on its own line.)

// NG
if (!Number.isInteger(length)
  || length < 0) {
  // ...
}

// OK
if (
  !Number.isInteger(length)
  || length < 0
) {
  // ...
}

Write one method per line in a method chain

object the code is already inside, and it is the fixed name /hoc-higher-order-functions gives the

current item of a higher-order callback. Neither carries information of its own, so counting

either would chop a line that states one receiver and one member — this.converter.toFixed({ value })

and it.name.toUpperCase() each say one thing, and each stays on one line.

meaning. manifest.env.isProduction() is two receivers on one line, and chopping it is not

the fix — see the next bullet.

grows the member that reaches what is behind it, so the line names one receiver and nothing

has to be chopped. Destructuring the inner object out is not the fix: an instance is never

taken apart — see /hoc-statements.

```javascript

// Two receivers

.filter(it => it.account.isActive())

// One: the item answers for itself

.filter(it => it.hasActiveAccount())

```

of a property is yours to do only inside the top-receiver, which is why this. reaches one

further and a named object does not. Where the line is manifest.env.isProduction(), what the

count is reporting belongs to /hoc-manifest-pattern.

the same knowledge of the manifest's insides that manifest.env did, one line higher up.

An item that can only be taken apart, because it has no member to answer with, is a plain

object — and why that narrows the design belongs to /hoc-classes-principles.

callback item's field never shows up there at all. What the count catches is a subset; the

design conventions judge the rest.

expressions on the line governs it too, and a chain can pass this rule while failing that one.

Read both before deciding where a chain breaks.

// NG
const ids = this.extractSamples().filter(it => it).map(it => it.id)

// NG (not one method per line)
const ids = this.extractSamples()
  .filter(
    it => it
  ).map(
    it => it.id
  )

// OK
const ids = this.extractSamples()
  .filter(it => it)
  .map(it => it.id)

Where the chain itself carries the complex expressions, chop it

**A receiver derived by a call, or reached through a computed key, is a complex expression of

its own** — and the method called on it is a second. One receiver and one method sit within

the count this section states, so this rule permits the line; the rule against two complex

expressions on one line is what forbids it.

The fix there is to chop the chain, not to name a variable. Chopping already secures the

readability the naming would have bought, and introducing a variable for what a line break

handles is what the rule against meaningless assignments turns away.

// NG: two calls on one line — the key's, and the method's
return entity[resolveKey(this.ModelCtor.name)].toSorted(this.getSorter())

// NG: a variable that chopping made unnecessary
const key = resolveKey(this.ModelCtor.name)

return entity[key]
  .toSorted(this.getSorter())

// OK: the chain carries them, so the chain is where it breaks
return entity[resolveKey(this.ModelCtor.name)]
  .toSorted(this.getSorter())

One-line and chopped chains stand together

A file holding both is not inconsistent for holding both. Every line is decided by what it

carries, so two chains of the same length break differently the moment one of them derives its

receiver and the other does not.

// One line: the receiver is a plain identifier, and the method is the only call
const sorted = array.toSorted(sorter)

// Two lines: the receiver is a call, so the method is the second one on the line
const [latest] = this.extractStatuses()
  .toSorted(comparer)

Reading the shorter one as a breach of the longer one's shape is the mistake this note exists

to prevent. Where the two differ, look for what the receiver costs before calling it a

divergence.

Write one property per line in a property chain

this.xxxx and it.xxxx each stand as one receiver.

// NG
const id = this.entity.comment.author.id

// OK
const username = this.entity.username

const id = this.entity.comment
  .author
  .id

Chop down when the same punctuation mark repeats consecutively

// NG
console.log(Math.round(average(SCRIPTS.filter(it => it.living)
  .map(it => it.year))))

// OK
console.log(
  Math.round(
    average(
      SCRIPTS.filter(it => it.living)
        .map(it => it.year)
    )
  )
)

For function calls with two or more arguments, write one argument per line

// NG: two arguments on the same line
this.delimiter.replace(specialCharacterPattern, '\\$<special>')

// OK: one argument per line
this.delimiter.replace(
  specialCharacterPattern,
  '\\$<special>'
)

Do not make meaningless variable assignments when chop down alone secures readability

// NG: assigning to a meaningless variable when chop down alone would suffice
const specialCharacterPattern = /(?<special>[.*+?^${}()|[\]\\])/ug

return this.delimiter.replace(
  specialCharacterPattern,
  '\\$<special>'
)

// OK: secure readability with chop down, without a variable assignment
return this.delimiter.replace(
  /(?<special>[.*+?^${}()|[\]\\])/ug,
  '\\$<special>'
)

Do not write two or more complex expressions on one line

// NG: two complex expressions on one line (two method calls)
return `${segment.charAt(0).toUpperCase()}${segment.slice(1).toLowerCase()}`

// OK: split each into its own const
const head = segment.charAt(0)
  .toUpperCase()
const tail = segment.slice(1)
  .toLowerCase()

return `${head}${tail}`

Do not create unnecessary intermediate strings

// NG: creating an intermediate string with toLowerCase() and then calling replaceAll (2 passes)
return text.toLowerCase()
  .replaceAll(pattern, replacer)

// OK: convert with a single replaceAll
return text.replaceAll(pattern, replacer)

Use template literals for string concatenation

// NG
return head
  + tail

// OK
return `${head}${tail}`

Regular expression literal flags

// NG: missing the u flag
const pattern = /[A-Z]/g

// OK: u first, the rest in alphabetical order (g → i)
const pattern = /[A-Z]/ug
const another = /[a-z]/ugi
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.