Skip to content

Class Architecture & Returns

Specification > Engineering

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.


RuleRequirement
ParametersA public method MUST accept a single object parameter.
ReturnsA method MUST return an object; it MUST NOT return a bare primitive.
DestructuringA method MUST destructure its payload on its first line.
SpreadYou 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.

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.

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 input
const limit = options.limit || 10
// Required — an explicit, allowed default
const { limit = 10 } = options
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.