prefer-functions
When to write a class instead of functions.
The rule
Agents read this text as written when they write and review code.
Default to functions. Write a class if, and only if, at least one of these holds:
- (a) Mutable state persists across method calls: a
RateLimitercounting its remaining tokens, a cache, a connection pool. - (b) Three or more operations share injected config or dependencies: an
HttpClientwhose base URL, retries and credentials are injected once and used by every method. - (c) Several implementations share one interface:
FileSourceandS3Sourcebehind aRecordSourcecontract. - (d) A framework requires it, because its dependency injection needs classes.
When none holds, write functions in a module. To check, ask whether "how many of these exist right now?" is a meaningful question: two HttpClients pointed at different APIs, yes, so a class; two formatDates, no, so a function.
Two shapes never pass that test:
- A class with only static members. It is a module in disguise: it adds a
ClassName.prefix to every call and binds no state. Write module functions instead. - A stateless class with one method, such as
class ReportGenerator { execute() }. Write the function.
A class that is decorated, is abstract, or implements or extends another type is not one of these shapes; the four criteria above judge it.
Keep a class's methods to behaviour that needs its state. Put other logic in functions rather than instance methods: logic that needs no state is easier to read and reuse as a function.
Examples
The check flags the incorrect code and passes the correct code; the agent judges what the check cannot see. Each example is a small repo, because this rule looks across files. It opens on the file that matters; the other files are the repo around it.
Incorrect
interface ConstructorParams {locale: string;}// None of the four criteria hold: nothing persists between calls, one operation// uses the injected value, there is no second implementation and no framework// asked for a class. "How many PriceFormatters exist?" is not a question.export class PriceFormatter {private readonly locale: string;constructor({ locale }: ConstructorParams) {this.locale = locale;}format({ amount }: { amount: number }): string {return amount.toLocaleString(this.locale);}}
Correct
interface ConstructorParams {tokens: number;}// Criterion (a): the remaining tokens persist across calls, so "how many rate// limiters exist right now?" is a question with a meaningful answer.export class RateLimiter {private remaining: number;constructor({ tokens }: ConstructorParams) {this.remaining = tokens;}take(): boolean {const allowed = this.remaining > 0;if (allowed) {this.remaining -= 1;}return allowed;}refill({ tokens }: { tokens: number }): void {this.remaining = tokens;}}
Configure
- Block
"blocking"Stops a run when a file the run changed breaks the rule. - AdviseDefault
"advisory"Reports it and hands it to the refactor agent. Never stops a run. - Off
"off"Not checked. Use it when your own linter already enforces the rule.
Add this to your lightsout.config.json, then change the value.
{"standards-rule-settings": {"prefer-functions": "advisory"}}