CodePark/ questions / When not to use classes
START PRACTICINGPRACTICE
Backend question · Object-oriented programming › When not to use classes

A code review finds export class StringUtils { static slugify(s: string) {...} static truncate(s: string, n: number) {...} }. It has no state. What is the usual suggestion?

Export slugify and truncate as plain functions from a module

Export plain functions. The module is already the namespace: import { slugify } from "./strings". Unused functions can be removed by bundlers, and there is nothing to construct.

"Turn it into a singleton with
getInstance()" is tempting, but it adds global state and ceremony to code that has no state at all. Injecting it is only worth it when you need to swap the implementation, which pure string helpers never need.