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
Avoid creating anemic exceptions as globals.
Create exceptions only if they behave differently.
Model exceptions with objects. Classes are handy for lazy programmers.
Sample Code
Wrong
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReader {
public static void main(String[] args) {
FileReader file = null;
try {
file = new FileReader("source.txt");
file.read();
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(FileLockedException e) {
e.printStackTrace();
}
catch(FilePermissionsException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
finally {
try {
file.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
}
Right
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReader {
public static void main(String[] args) {
FileReader file = null;
try {
file = new FileReader("source.txt");
file.read();
}
catch(FileException exception) {
if (exception.description == (this.expectionMessages().errorDescriptionFileTemporaryLocked() {
//sleep and retry
//IF behaviour is the same with all the exceptions just change the text on object creation and raise the icorrect instance
}
this.showErrorToUser(exception.messageToUser();
//This example is simplified. Text should be translated
}
finally {
try {
file.close();
}
}
}
}
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
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Code Smell 01 - Anemic Models
Maxi Contieri ・ Oct 20 '20
Credits
Photo by Nick van den Berg on Unsplash
You will fall to ruin because you believe that exceptions to the rule make new rules.
Pierce Brown
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21
Last update: 2021/07/13
Top comments (0)