cover-img

Code Smell 26 - Exceptions Polluting

Don't create anemic and empty objects. Even if they are Exceptions

19 November, 2022

4

4

0

It is very nice to have lots of different exceptions. Your code is declarative and robust. Or not?

TL;DR; Don't create anemic and empty objects. Even if they are Exceptions.

Problems

Over Design

Namespace Pollution

Solutions

1.

Avoid creating anemic exceptions as globals.

2.

Create exceptions only if they behave differently.

3.

Model exceptions with objects. Classes are handy for lazy programmers.

Sample Code

Wrong

Right

Detection

New exceptions should override behavior methods.
No. code, description, resumable, etc. are not behavioral.

Tags

Abuser

Naming

Conclusion

You would not create different classes for every Person instance, so they return different names. Why would you do it with exceptions.
How often do you catch a specific exception?.
Go out and check your code.
Is it necessary to be a class?
You are already coupled to the class. Couple to the description instead.
Exception instances should NOT be Singletons.

Relations

Credits

You will fall to ruin because you believe that exceptions to the rule make new rules.

Pierce Brown
This article is part of the CodeSmell Series.

4

4

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.