Skip to main content

Command Palette

Search for a command to run...

Code Smell 184 - Exception Arrow Code

Arrow code is a code smell. Exception polluting is another. This is a mortal combination.

Updated
2 min read
Code Smell 184 - Exception Arrow Code
M

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

TL;DR: Don't cascade your exceptions

Problems

  • Readability

  • Complexity

Solutions

  1. 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)) {
             throw new I0Exception("File exists: " + filename);
        }
        this.SaveOnValidFilename(filename);
    }
}

Detection

[X] Semi-Automatic

Some linters warn us when we have this kind of complexity

Tags

  • Exceptions

Conclusion

Exceptions are less critical than normal cases.

If we need to read more exceptional code than normal then it is time to improve our code.

Relations

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Remy Gieling on Unsplash


An error doesn't become a mistake until you refuse to correct it.

Orlando Aloysius Battista


This article is part of the CodeSmell Series.

G

I really enjoy these posts. Thank you for taking the time to do this work.

An understandable refactoring error, but in the problem code you only save if the file doesn't exist, but in the refactored code you left in the negation, meaning if the file doesn't exist you raise a File Exists exception.

I'll delete myself if I've read that wrong :).

1
M

Amazing!

Love this kind of comments. It was a refactoring mistake due to the lack of tests. And nobody noticed it before.

Fixed. Thank you so much!

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.