cover-img

Code Smell 32 - Singletons

The most used and (in)famous design pattern in the world is causing us great harm.

29 November, 2022

5

5

0

The most used and (in)famous design pattern in the world is causing us great harm.

Problems

Coupling

Testability

Accidental problems

Multi threading

Premature Optimization

Solutions

1.

Avoid it.

2.

Use contextual unique objects.

3.

Benchmark object creation.

Examples

Database Access

Globals

Logging

Sample Code

Wrong

Right

Detection

This is a design pattern. We should avoid it by policy.
We can add linter rules for patterns like 'getInstance()' so new developers cannot infect code with this anti-pattern.

Tags

Globals

Conclusion

This is an historical mistake already acknowledged by the community. Nevertheless, lazy developers bring it again and again. We need to reach a consensus on its drawbacks.

Relations

More Info

Credits

The Diagram is Not the Model. The model is not the diagram. It is an abstraction, a set of concepts and relationships between them.

Eric Evans
This article is part of the CodeSmell Series.

5

5

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.