Class Architecture & Returns
This chapter defines how classes expose behaviour: object-shaped method signatures, object-shaped returns, private-by-default members, and the absence of silent fallbacks. It is conditional and applies to the JavaScript sources covered by the stack chapter.
Method Signatures & Returns
Section titled “Method Signatures & Returns”| Rule | Requirement |
|---|---|
| Parameters | A public method MUST accept a single object parameter. |
| Returns | A method MUST return an object; it MUST NOT return a bare primitive. |
| Destructuring | A method MUST destructure its payload on its first line. |
| Spread | You MUST NOT use the spread operator to assemble parameters or returns. |
Returning an object keeps call sites stable: a new field can be added to the return value without touching every caller.
Visibility
Section titled “Visibility”Members MUST be private by default. You MUST expose only the methods that form the class’s contract and keep all helpers and internal state private.
No Silent Defaults
Section titled “No Silent Defaults”You MUST NOT supply a fallback with || for a missing value. A default MUST be explicit and intentional; absence MUST surface as an error or as a documented, allowed default, never as a silently coerced value.
// Prohibited — a silent fallback hides a missing inputconst limit = options.limit || 10
// Required — an explicit, allowed defaultconst { limit = 10 } = optionsExample
Section titled “Example”class PriceList { static calculate( { items, currency } ) { const total = items.reduce( ( sum, item ) => sum + item.price, 0 ) return { total, currency } }}The method takes one object, destructures it immediately, iterates with an array method, and returns an object rather than a bare number.