Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 26 — Exceptions Polluting

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

Photo by Nick van den Berg on Unsplash

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

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

Pierce Brown

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

Responses (1)

Write a response

> Couple to the description instead
This sounds like a very bad idea. The whole reason we avoid coupling to a description is so we can leave the library authors free to update the description as they please (fix a spelling/grammer mistake, add some…

--