Pass the essence to all your objects so they will not need to mutate.Pass the essence to all your objects so they will not need to mutate.

Code Smell 13 - Empty Constructors

2026/01/13 08:36
3 min di lettura
Per feedback o dubbi su questo contenuto, contattateci all'indirizzo crypto.news@mexc.com.

Non-parameterized constructors are a code smell of an invalid object that will dangerously mutate. Incomplete objects cause lots of issues.

Problems 😔

  • Mutability
  • Incomplete objects
  • Concurrency inconsistencies between creation and essence setting.
  • Setters

Solutions 😃

  1. Pass the object's essence on creation
  2. Create objects with their immutable essence.

\ Refactorings ⚙️

https://hackernoon.com/refactoring-remove-setters-codesmell?embedable=true

https://hackernoon.com/refactoring-016-building-with-the-essence?embedable=true

Examples 📚

  • Some persistence frameworks in statically-typed languages require an empty constructor.

Sample Code 📖

Wrong 🚫

class AirTicket { constructor() { } }

class AirTicket { constructor(origin, destination, arline, departureTime, passenger) { // ... } }

Detection 🔍

Any linter can warn about this (possible) situation.

Exceptions 🛑

  • Stateless objects. Always a better solution than static class methods.

Tags 🏷️

  • Anemic Models

Level 🔋

[X] Beginner

Why the Bijection Is Important

In the MAPPER, objects correspond to real-world entities.

\ Real people aren't born nameless and formless, then gradually acquire attributes.

\ You don't meet someone who temporarily has no age or email address.

\ When you model a person, you should capture their essential attributes at birth, just like reality.

\ Breaking this bijection by creating hollow objects forces you to represent impossible states.

\ Empty constructors create phantom and invalid objects that don't exist in your domain model, violating the mapping between your code and reality.

AI Generation 🤖

AI code generators frequently produce this smell because they often follow common ORM patterns.

\ When you prompt AI to "create a Person class," it typically generates empty constructors with getters and setters.

\ AI tools trained on legacy codebases inherit these patterns and propagate them unless you explicitly request immutable objects with required constructor parameters.

AI Detection 🧲

AI tools can detect and fix this smell when you provide clear instructions.

\ You need to specify that objects should be immutable with required constructor parameters.

\ Without explicit guidance, AI tools may not recognize empty constructors as problematic since they appear frequently in training data.

Try Them! 🛠

Remember: AI Assistants make lots of mistakes

Without Proper Instructions 📵

  • ChatGPT
  • Claude
  • Perplexity
  • Copilot
  • You
  • Gemini
  • DeepSeek
  • Meta AI
  • Grok
  • Qwen

With Specific Instructions 👩‍🏫

  • ChatGPT
  • Claude
  • Perplexity
  • Copilot
  • You
  • Gemini
  • DeepSeek
  • Meta AI
  • Grok
  • Qwen

Conclusion 🏁

Always create complete objects. Make their essence immutable to endure through time.

\ Every object needs its essence to be a valid one since its inception.

\ We should read Plato's ideas about immutability and create entities in a complete and immutable way.

\ These immutable objects favor bijection and survive the passing of time.

Relations 👩‍❤️‍💋‍👨

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-vi-cmj31om

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-viii-8mn3352

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ii-o96s3wl4?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxiv

More Information 📕

https://hackernoon.com/is-it-crystal-clear-for-everybody-that-a-date-should-not-mutate-wuoy3z03?embedable=true

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-ii-o96s3wl4?embedable=true

Credits 🙏

Photo by Brett Jordan in Pexels


Joel Spolski

https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true


This article is part of the CodeSmell Series.

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-i-xqz3evd

\

Opportunità di mercato
Logo Notcoin
Valore Notcoin (NOT)
$0.0003543
$0.0003543$0.0003543
-0.64%
USD
Grafico dei prezzi in tempo reale di Notcoin (NOT)
Disclaimer: gli articoli ripubblicati su questo sito provengono da piattaforme pubbliche e sono forniti esclusivamente a scopo informativo. Non riflettono necessariamente le opinioni di MEXC. Tutti i diritti rimangono agli autori originali. Se ritieni che un contenuto violi i diritti di terze parti, contatta crypto.news@mexc.com per la rimozione. MEXC non fornisce alcuna garanzia in merito all'accuratezza, completezza o tempestività del contenuto e non è responsabile per eventuali azioni intraprese sulla base delle informazioni fornite. Il contenuto non costituisce consulenza finanziaria, legale o professionale di altro tipo, né deve essere considerato una raccomandazione o un'approvazione da parte di MEXC.

$30,000 in PRL + 15,000 USDT

$30,000 in PRL + 15,000 USDT$30,000 in PRL + 15,000 USDT

Deposit & trade PRL to boost your rewards!