Arrow code is a code smell. Exception polluting is another. This is a mortal combination.
TL;DR: Don't cascade your exceptions
Problems
Readability
Complexity
Solutions
- Rewrite the nested clauses
Context
In the same way arrow code is hard to read, handling exceptions is a usual case when we must address the topics in a cascade way.
Sample Code
Wrong
class QuotesSaver {
public void Save(string filename) {
if (FileSystem.IsPathValid(filename)) {
if (FileSystem.ParentDirectoryExists(filename)) {
if (!FileSystem.Exists(filename)) {
this.SaveOnValidFilename(filename);
} else {
throw new I0Exception("File exists: " + filename);
}
} else {
throw new I0Exception("Parent directory missing at " + filename);
}
} else {
throw new ArgumentException("Invalid path " + filename);
}
}
}
Right
public class QuoteseSaver {
public void Save(string filename) {
if (!FileSystem.IsPathValid(filename)) {
throw new ArgumentException("Invalid path " + filename);
} else if (!FileSystem.ParentDirectoryExists(filename)) {
throw new I0Exception("Parent directory missing at " + filename);
} else if (FileSystem.Exists(filename))