prefer-plain-objects-over-classes
Installation
SKILL.md
Summary
Prefer Plain Data Objects (interfaces/types) and Standalone Functions over class definitions. Classes are difficult for bundlers to minify (property names are preserved) and tree-shake (methods are attached to the prototype, appearing "used").
Rationale
- Tree-Shaking: Tools like Webpack or Rollup often cannot remove unused methods from a class because they are attached to the prototype dynamically. Standalone functions are easily eliminated if unused.
- Minification: Class property names and method names usually cannot be mangled (shortened) safely. Function names/variables are aggressive compilation targets for minifiers.
- Simplicity: Separating data from behavior (Functional Programming style) often leads to more reusable code.
Guidance
- Data: Use
interfaceortypeto define the shape of your data. - Constructor: Use a simple factory function (
createFoo(...)) that returns the plain object. - Methods: Write functions that take the object as the first argument (
doSomething(foo, ...)).