banned-class-shapes
a class that is only static members, or one stateless method
Deterministic checkadvisory by defaultbasecode
The argument
Banned:
- Static-only classes — a module wearing a costume; it adds
ClassName.prefixes and binds no state. Use module functions (each exported function in its own file). - One-method stateless classes —
class ReportGenerator { execute() }is a function with a hat on. Write the function.
The proof
fail
// A module wearing a costume: the class binds no state and exists only to put
// a `ConfigPaths.` prefix in front of three values.
export class ConfigPaths {
static readonly root = '.lightsout';
static readonly config = '.lightsout/config.json';
static forRun({ id }: { id: string }): string {
return `.lightsout/runs/${id}`;
}
}
pass
interface ConstructorParams {
baseUrl: string;
retries?: number;
}
// Criteria (a) and (b) together: injected configuration every method reads, and
// a retry budget that changes as calls are made.
export class HttpClient {
private readonly baseUrl: string;
private remaining: number;
constructor({ baseUrl, retries = 3 }: ConstructorParams) {
this.baseUrl = baseUrl;
this.remaining = retries;
}
get({ path }: { path: string }): string {
this.remaining -= 1;
return `GET ${this.baseUrl}${path}`;
}
post({ path }: { path: string }): string {
this.remaining -= 1;
return `POST ${this.baseUrl}${path}`;
}
getRemainingRetries(): number {
return this.remaining;
}
}
Turn it down
Both lines go in your lightsout.config.json.
"standards-checks": { "banned-class-shapes": "advisory" }"standards-checks": { "banned-class-shapes": "off" }