Unnecessary Default Constructor
ID |
javascript.unnecessary_default_constructor |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
JavaScript |
Tags |
code-style, dead-code |
Description
Reports class constructors that the engine would already insert implicitly:
-
An empty constructor on a class without an
extendsclause. -
A derived class constructor whose body is a single
super(…)call that just forwards every parameter to the superclass.
In either case the explicit constructor adds no behaviour and is just noise.
Rationale
JavaScript automatically generates a constructor when a class declares none — constructor() {} for non-derived classes, and constructor(…args) { super(…args); } for derived classes. Writing the same constructor by hand:
-
Adds nothing the runtime doesn’t already do.
-
Bloats the source for readers and tooling.
-
Drifts over time — someone adds an irrelevant comment / parameter to the explicit form, others assume it has a purpose, and the class accumulates unnecessary scaffolding.
// Bad
class A {
constructor() {}
}
class B extends Base {
constructor(a, b) {
super(a, b);
}
}
class C extends Base {
constructor(...args) {
super(...args);
}
}
Remediation
Remove the redundant constructor entirely. The implicit one is created automatically.
// Good
class A {}
class B extends Base {}
class C extends Base {}
If the constructor needs to do real work (initialise state, transform parameters, validate input), keep it — the rule only flags constructors that match the implicit default exactly.